如何获得UILabel以显示多行和自动换行?

时间:2019-02-03 04:05:14

标签: 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中很容易做到这一点,但我正在尝试使其以编程方式工作。

2 个答案:

答案 0 :(得分:1)

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

为此,我将UILabel()的约束(例如top,bottom,lead,所有约束拖到0)赋予了

在UITableView()中,有一个称为的方法:

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

尝试一下,我想它一定会对您有帮助。

答案 1 :(得分:0)

在我的情况下,部分解决方案是Varsha Shivhare建议的,另一种是将heightAnchor约束设置为> = 60,这是我现在正在使用的约束。谢谢!