ios - 是否可以在 UITableViewHeaderFooterView 中使用多行文本标签(不使用自定义 UILabel)?

标签 ios iphone uitableview swift

我正在尝试在 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 是我唯一的选择吗?

非常感谢!

最佳答案

我确实认为使用自定义 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
}

然后在您的 View Controller 中,预先计算部分标题的高度

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
}

关于ios - 是否可以在 UITableViewHeaderFooterView 中使用多行文本标签(不使用自定义 UILabel)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27902357/

相关文章:

iphone - 您可以在 iPhone 应用程序的首选项中添加一个按钮吗?

iPhone:如何获取使用 UIImageWriteToSavedPhotosAlbum() 保存的图像的文件路径?

ios - 使用异步数据更新 TableViewCell

ios - 从 TableView 为索引部分和行传递正确的 Realm 对象时出现问题

ios - 如何将函数传递给另一个设置其完成闭包的函数?

ios - Swift 4 - 从 UICollectionView 单元格推送 ViewController

iphone分配实例变量

ios - 按下自定义按钮时如何在 UITableView 中查找索引路径

ios - 重新限制 UIButton 的宽度

ios - 创建 iOS 框架 : how to prevent insight into source code while debugging