0

我试图让我的 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 中很容易做到这一点,但我试图让它以编程方式工作。

4

2 回答 2

1

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

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

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

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

试试这个,我猜它肯定会帮助你。

于 2019-02-03T15:56:34.090 回答
0

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

于 2019-02-03T20:52:40.157 回答