ios - 如何让 UILabel 显示多行和自动换行?

标签 ios swift uilabel swift5

我试图让我的 itemLabel UILabel 成为多行并进行自动换行,但我无法弄清楚我哪里出错了。我设置了 numberOfLines = 0 和 .byWordWrapping。我确定这是我弄乱的一行简单代码,但我找不到它。

import UIKit
import Foundation

protocol ListItemCellDelegate {
    func setChecked(cell: ListItemCell)
}

class ListItemCell: UITableViewCell {
    var delegate: ListItemCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        self.delegate = nil
    }

    let cellView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.setCellShadow()
        return view
    }()

    let checkButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "UnChecked"), for: .normal)
        button.setImage(UIImage(named: "Checked"), for: .selected)
        button.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        return button
    }()

    let priorityButton: UIButton = {
        let button = UIButton(type: .custom)
        button.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
        button.setTitleColor(.black, for: .normal)
        button.backgroundColor = UIColor.white
        button.layer.borderWidth = 2
        button.layer.borderColor = UIColor.black.cgColor
        button.titleLabel?.font = UIFont.init(name: "Avenir Next", size: 24)
        return button
    }()

    let itemLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.black
        label.font = UIFont.init(name: "Avenir Next", size: 16)
        return label
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupCell()

        checkButton.addTarget(self, action: #selector(setChecked), for: .touchUpInside)
        itemLabel.translatesAutoresizingMaskIntoConstraints = false
        itemLabel.numberOfLines = 0
        itemLabel.lineBreakMode = .byWordWrapping
    }

    func setupCell() {
        addSubview(cellView)
        cellView.addSubview(checkButton)
        cellView.addSubview(priorityButton)
        cellView.addSubview(itemLabel)

        cellView.setAnchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 4, paddingLeft: 8, paddingBottom: 4, paddingRight: 8)
        checkButton.setAnchor(top: nil, left: cellView.leftAnchor, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 50, height: 50)
        checkButton.centerYAnchor.constraint(equalTo: cellView.centerYAnchor).isActive = true
        priorityButton.setAnchor(top: nil, left: checkButton.rightAnchor, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 40, height: 40)
        priorityButton.centerYAnchor.constraint(equalTo: checkButton.centerYAnchor).isActive = true
        itemLabel.setAnchor(top: nil, left: priorityButton.rightAnchor, bottom: nil, right: rightAnchor, paddingTop: 0, paddingLeft: 20, paddingBottom: 0, paddingRight: 20)
        itemLabel.centerYAnchor.constraint(equalTo: priorityButton.centerYAnchor).isActive = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @objc private func setChecked() {
        self.delegate?.setChecked(cell: self)
    }
}

我希望 UILabel 将文本换行并占用尽可能多的空间以适应单元格中的整个标签文本。现在它不显示整个消息,只显示大小允许的大小。但是,我希望它根据字符串的长度调整大小。我在 Storyboard 中很容易做到这一点,但我试图让它以编程方式工作。

最佳答案

我以前做过,就像在 UITableView() 中有一个字符串数组,字符串的长度不固定,它可以是 3 行或更多行。

因此,为此,我已将约束条件赋予 UILabel(),例如顶部、底部、前导、尾随所有约束都为 0。

在 UITableView() 中,有一个方法叫做:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
返回 UITableViewAutomaticDimension

试试这个,我猜它一定会对你有帮助。

关于ios - 如何让 UILabel 显示多行和自动换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54499852/

相关文章:

ios - 无法将数据传递给 viewdidload,Swift

ios - 如何以 00 :00:00 using AVFundation 格式打印音频时间

ios - 如何在 iOS 中识别表格行?

ios - 确保 iOS 中的符号 th 正确显示

ios - Objective-c:在 NSArray 上没有得到正确的文本

ios - UITableView 标题背景颜色没有完全改变

ios - 将庞大的 Firebase 数据库作为事务更新

ios - UILabel 不显示所有文本

ios - 打开 facebook 页面进入 facebook 应用 Ios + ionic

swift - 在 Swift 中从顶部裁剪图像