Search code examples
iosswiftuitableviewuilabeltablefooterview

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:

tableFooterView

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.


Solution

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

    enter image description here