【问题标题】:How to place a dynamic multiline UILabel in tableHeaderView如何在 tableHeaderView 中放置动态多行 UILabel
【发布时间】:2019-04-18 09:38:48
【问题描述】:

我创建了一个 xib 视图,该视图链接到一个名为 InstructionView 的 UIView 类。该视图只包含一个 UILabel,该 UILabel 被限制在各个方向的边距。

标签的行数为0,换行设置为自动换行。

然后这个视图被添加到一个 stackView 中。然后那个stackView被设置为tableView的tableHeaderView。

override func viewsToAddAfterTitle() -> [UIView] {
    var views: [UIView] = []
    if let view = Bundle.main.loadNibNamed(Constants.Nibs.instructionView, owner: self, options: nil)?.first as? InstructionView {
        view.fillWithInstruction("A long text that needs more than 1 line")
        views.append(view)
        view.setNeedsLayout()
        view.layoutIfNeeded()
    }
    return views
}

func addTableHeaderView() {
    if let viewHeader: ViewHeader = Bundle.main.loadNibNamed(Constants.Nibs.viewHeader, owner: self, options: nil)?.first as? ViewHeader {
        viewHeader.setTitle(titleText: headerTitle())
        var arrangedStackSubviews: [UIView] = viewsToAddBeforeTitle()
        arrangedStackSubviews.append(viewHeader)
        arrangedStackSubviews.append(contentsOf: viewsToAddAfterTitle())
        let stack = UIStackView(arrangedSubviews: arrangedStackSubviews)
        stack.distribution = .fill
        stack.axis = NSLayoutConstraint.Axis.vertical
        stack.alignment = .fill
        stack.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        tableView.tableHeaderView = stack
    }
}

fillWithInstruction 函数在我加载笔尖时被调用。我调用它来设置标签的文本。

func fillWithInstruction(_ instruction: String) {
    instructionLabel.text = instruction
    instructionLabel.sizeToFit()
}

我尝试计算intrinsicContentSize 并在此基础上设置标签高度的约束。我尝试将标签放在视图和/或堆栈中。

我尝试过的都没有奏效。如果该文本需要超过 1 行,我希望标签显示超过 1 行。但目前它始终显示 1 行。

【问题讨论】:

  • 也许尝试在 func viewsToAddAfterTitle() 中以编程方式添加行数作为 theLabel.numberOfLines = 0

标签: swift uilabel multiline


【解决方案1】:

创建一个 UITableViewHeaderFooterView 并将其与 viewForHeaderInSection 委托一起使用。

看看这个例子

Custom header view from xib for UITableView

【讨论】:

  • 问题是专门询问如何在表格视图标题中进行操作,而不是在节标题中。
【解决方案2】:

好的,这是真正的交易:

创建这个扩展:

extension String {
func heightNeededForLabel(_ label: UILabel) -> CGFloat {
    let width = label.frame.size.width
    guard let font = label.font else {return 0}
    let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
    let boundingBox = self.boundingRect(with: constraintRect, options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [NSAttributedString.Key.font: font], context: nil)
    return boundingBox.height + 20
}

}

+20 只是为了给它更多空间

然后这样称呼它:

func fillWithInstruction(_ instruction: String) {
    instructionLabel.text = instruction
    guard let instructionLabel = instructionLabel else {return}
    instructionLabel.addConstraint(NSLayoutConstraint(item: instructionLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 2, constant: instruction.heightNeededForLabel(instructionLabel)))

}

【讨论】:

    猜你喜欢
    • 2016-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多