【问题标题】:line breaks not working on UILabel in tableFooterView换行符不适用于 tableFooterView 中的 UILabel
【发布时间】:2020-07-08 02:10:26
【问题描述】:

我拥有一个带有 footerViewtableView。它应该显示一个简单的标签。

在我的 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 方法,AutoLayout 可以正常工作。唯一的问题是标签高度只有一行。

【问题讨论】:

    标签: ios swift uitableview uilabel tablefooterview


    【解决方案1】:

    HERE 是您为tableHeaderView 实现它的方式,您只需在UIViewController 类中添加以下代码

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

    还有助手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
            }
        }
    }
    

    并删除

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

    以上代码。

    结果将是:

    【讨论】:

    • 谢谢。但这不适用于动态类型。字体会相应改变,但高度不会改变。
    • 编辑:它适用于dynamicType,我只需要重新加载tableView
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    相关资源
    最近更新 更多