调整tableView的tableHeaderView的大小以考虑attributesText?

时间:2018-04-27 23:26:21

标签: ios swift uitableview

我想确保headerView内的tableView动态增长,以说明我的属性字符串的大小。

这个概念适用于我的单元格视图的自动布局,没有太多工作。

但是我遇到了标题(UIView)的问题,所以我试图用手计算它的高度,但由于某种原因,这段代码给出的高度太小了?

 func setupTableHeader () {
    let str = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"
    let attributedStr = NSAttributedString(string: str)
    headerViewLabel.attributedText = attributedStr

    let rect = attributedStr.boundingRect(with: CGSize(width: view.frame.width, height: 100000000000000000), options: [.usesFontLeading, .usesLineFragmentOrigin], context: nil)

    tableView.tableHeaderView = headerView
    let height = rect.height
    headerView.frame = CGRect(x: 0, y: 0, width: self.view.width, height: height + headerView.frame.height)

}

2 个答案:

答案 0 :(得分:0)

NSAttributedString上创建一个基于给定宽度计算高度的扩展名:

extension NSAttributedString {
    func height(forConstantWidth width: CGFloat) -> CGFloat {
        let constraintBox = CGSize(width: width, height: .greatestFiniteMagnitude)
        let calculatedBox = boundingRect(with: constraintBox, options: .usesLineFragmentOrigin, context: nil)
        return calculatedBox.height
    }
}

您可以调用它并传入表视图的宽度,例如:

let height = myAttrString.height(forConstantWidth: tableView.frame.size.width)

答案 1 :(得分:0)

您可以调整UITableView's标题大小以考虑其子元素的大小。

您可以使用以下扩展名。

extension UITableView {
    func automaticHeaderViewSize() {
        if let headerView = self.tableHeaderView {

            let height = headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
            var headerFrame = headerView.frame

            //Comparison necessary to avoid infinite loop
            if height != headerFrame.size.height {
                headerFrame.size.height = height
                headerView.frame = headerFrame
                self.tableHeaderView = headerView
            }
        }
    }
}

请确保您的标签有numberOfLines = 0

所以,最后你的代码将会是(根据你的问题)。

 func setupTableHeader () {
    let str = "your long text..."
    let attributedStr = NSAttributedString(string: str)
    headerViewLabel.attributedText = attributedStr
    tableView.tableHeaderView = headerView
    tableView.automaticHeaderViewSize()
}