【问题标题】:Is it possible to have a multiline textLabel in a UITableViewHeaderFooterView (without using a custom UILabel)?是否可以在 UITableViewHeaderFooterView 中有一个多行 textLabel(不使用自定义 UILabel)?
【发布时间】:2015-03-10 05:47:09
【问题描述】:

我正在尝试在UITableViewHeaderFooterView 中使用内置的textLabel,以便在UITableView 的部分标题中显示标题。

这些标题的内容数量未知,因此需要覆盖多行。

如果这是一个表格单元格,那么myCell.numberOfLines = 0 将起作用(与estimatedHeightForRowAtIndexPath 一起返回UITableViewAutomaticDimension)。但我无法获得与使用表头类似的东西。

我尝试在viewForHeaderInSection 和/或willDisplayHeaderView 中设置textLabel.numberOfLines = 0。我还尝试在我创建的标题正在使用的自定义子类中设置它(使用let sectionHeader = tableView.dequeueReusableHeaderFooterViewWithIdentifier("myIdentifier") as MyTableSectionHeaderSubclass 设置)。在那个子类中,我尝试在init 函数以及layoutSubviews() 中设置textLabel.numberOfLines = 0

我已经通过计算文本字符串将占用的空间量设置了每个标题的正确高度(使用heightForHeaderInSection 中的CGSizeMake,如果有帮助,可以提供有关此的更多信息)。因此,标签有足够的垂直空间展开 - 它们只是卡在一行上,文本被截断并以省略号结尾。

我正在尝试这种方法以避免使用自定义 UILabel 来显示标题。虽然我可以通过这种方式应用多行,但这会带来其他问题,例如添加或删除表格行时标签位置/框架丢失。

有谁知道UITableViewHeaderFooterView 的内置textLabel 是否可以实现多行文本?还是自定义 UILabel 是我唯一的选择?

非常感谢!

【问题讨论】:

    标签: ios iphone uitableview swift


    【解决方案1】:

    我确实认为使用自定义 UILabel 是一种更好的方法,因为您可以控制所有属性。

    首先,一个方便的函数来计算 UILabel 的高度。 (以下是我的特定项目的版本)。请注意,我设置了NSMutableParagraphStyle,我认为这是处理换行符、行距等的最佳方式。

    internal func heightForLabel(attributedString:NSMutableAttributedString, font:UIFont, width:CGFloat, lineSpacing: CGFloat) -> CGFloat{
    
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpacing
    
        let label:UILabel = UILabel(frame: CGRect(x:0, y:0, width:width, height:CGFloat.greatestFiniteMagnitude))
        label.numberOfLines = 0
        label.lineBreakMode = .byWordWrapping
        label.font = font
        label.textAlignment = .left
        attributedString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))
        label.attributedText = attributedString
        label.sizeToFit()
    
        return label.frame.height
    }
    

    然后在您的视图控制器中,预先计算节标题高度

    fileprivate let sectionHeaders = ["Header1", "Loooooooooooooooooooooong Header which occupies two lines"]
    fileprivate var sectionHeaderHeights : [CGFloat] = []
    
    override func viewDidLoad() {
            super.viewDidLoad()
    
            //Calculate the label height for each section headers, then plus top and down paddings if there is any. Store the value to `sectionHeaderHeights`
    }
    

    UITableViewDelegate 方法

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return sectionHeaderHeights[section]
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let sectionHeader = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: sectionHeaderHeights[section]-paddings))
        sectionHeader.backgroundColor = .clear
    
        let sectionTitleLabel = UILabel()
        sectionTitleLabel.text = sectionTitles[section]
        sectionTitleLabel.font = UIFont(name: "GothamPro", size: 18)
        sectionTitleLabel.textColor = .black
        sectionTitleLabel.backgroundColor = .clear
        sectionTitleLabel.frame = CGRect(x: padding, y: sectionHeader.frame.midY, width: sectionTitleLabel.frame.width, height: sectionTitleLabel.frame.height)
        sectionHeader.addSubview(sectionTitleLabel)
    
        return sectionHeader
    }
    

    【讨论】:

      【解决方案2】:

      在 Interface Builder 药物原型单元格上方的 Label。在属性检查器中设置行数 = 0,换行符为自动换行并输入您的文本。

      【讨论】:

        【解决方案3】:

        使用这个:

        self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension
        

        请记住,自动调整大小仅适用于:

        lineBreakMode="tailTruncation" 
        numberOfLines="0"
        

        如果您这样做,请确保在您的单元格内,将 contentView 约束设置为与您的文本字段相关的顶部、左侧、底部、右侧。

        这样 iOS 会将单元格调整为文本字段大小。

        【讨论】:

        • 请多改进一点答案
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-24
        • 1970-01-01
        • 2010-12-22
        • 1970-01-01
        • 2013-04-02
        • 1970-01-01
        相关资源
        最近更新 更多