У меня есть tableView с footerView. Он должен отображать простую метку.
В моем ViewController в viewDidLoad
я назначаю tableFooterView так:
let footerView = MyFooterView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 0))
tableView.tableFooterView = footerView
MyFooterView
— это UIView с одной меткой. Настройка этикетки выглядит так:
label.font = someFont
label.adjustsFontForContentSizeCategory = true
label.textColor = .black
label.numberOfLines = 0
label.text = "my super looooooong label that should break some lines but it doesn't."
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
addSubview(label)
NSLayoutConstraint.activate([
label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 40),
label.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -40),
label.topAnchor.constraint(equalTo: self.topAnchor, constant: 20),
label.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -20)
])
Чтобы заставить AutoLayout работать с MyFooterView
, я вызываю этот метод внутри UIViewControllers viewDidLayoutSubviews
:
func sizeFooterToFit() {
if let footerView = self.tableFooterView {
footerView.setNeedsLayout()
footerView.layoutIfNeeded()
let height = footerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height
var frame = footerView.frame
frame.size.height = height
footerView.frame = frame
self.tableFooterView = footerView
}
}
Проблема. Линии в этикетке не прерываются. Я получаю следующий результат:
Что сделать, чтобы метка была многострочной? Автомакет работает благодаря методу sizeFooterToFit
. Единственное, высота меток не превышает одной строки.