简体   繁体   中英

line breaks not working on UILabel in tableFooterView

I habe a tableView with a footerView . It should display a simple label.

In my ViewController, in viewDidLoad , I assign the tableFooterView like so:

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. 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 :

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 . 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

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    tbl.updateHeaderViewHeight()
}

And Helper 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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM