如何在tableHeaderView中放置动态多行UILabel

时间:2019-04-18 09:38:48

标签: swift uilabel multiline

enter image description here我创建了一个链接到名为InstructionView的UIView类的xib视图。该视图只包含一个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()
}

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

我尝试的所有方法均无效果。如果文本需要多于1行,我希望标签显示多于1行。但是目前,它始终显示1行。

2 个答案:

答案 0 :(得分:0)

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

看看这个例子

Custom header view from xib for UITableView

答案 1 :(得分:0)

好的,这是真实的交易:

创建此扩展程序:

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

}