[英]line breaks not working on UILabel in tableFooterView
I habe a tableView with a footerView .我有一个带有footerView的tableView 。 It should display a simple label.它应该显示一个简单的标签。
In my ViewController, in viewDidLoad
, I assign the tableFooterView like so:在我的 ViewController 中,在viewDidLoad
,我像这样分配 tableFooterView:
let footerView = MyFooterView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 0))
tableView.tableFooterView = footerView
MyFooterView
is a UIView with a single label. MyFooterView
是一个带有单个标签的 UIView。 The label setup looks like so:标签设置如下所示:
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)
])
In order to get AutoLayout to work with MyFooterView
, I call this method inside UIViewControllers viewDidLayoutSubviews
:为了获得自动版式与工作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
}
}
Problem: The lines in the label do not break.问题:标签中的线条没有中断。 I get the following result:我得到以下结果:
What can I do so that the label has multiple lines?我该怎么做才能使标签具有多行? AutoLayout is working thanks to the method sizeFooterToFit
.由于方法sizeFooterToFit
AutoLayout 正在工作。 The only thing is that the labels height is only as high as a single line.唯一的问题是标签高度只有一行。
HERE is the way how you can achieve it for tableHeaderView
and with your case you just need to add below code in your UIViewController
class这是您如何为tableHeaderView
实现它的方法,对于您的情况,您只需要在UIViewController
类中添加以下代码
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
tbl.updateHeaderViewHeight()
}
And Helper extension
和助手extension
extension UITableView {
func updateHeaderViewHeight() {
if let header = self.tableFooterView {
let newSize = header.systemLayoutSizeFitting(CGSize(width: self.bounds.width, height: 0))
header.frame.size.height = newSize.height
}
}
}
And remove并删除
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
}
}
Above code.以上代码。
And result will be:结果将是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.