UILabel不作两行

问题描述 投票:0回答:2

我有一堆UILabel,但以下UILabel(lastMessage)仅显示一行。它应该被尾巴截断,但是没有省略号意味着第二行根本不存在。此外,dateTime UILabel也不显示。这可能与我的自动布局约束有关吗?我的UITableViewCell代码:

let nameLabel = UILabel()
    let lastMessage = UILabel()
    let profileImageView = UIImageView(image: #imageLiteral(resourceName: "blankAvatar"))
    let dateTime = UILabel()

    required init?(coder aDecoder: NSCoder) {super.init(coder: aDecoder)}
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        nameLabel.numberOfLines = 1
        nameLabel.tintColor = .label
        nameLabel.font = .boldSystemFont(ofSize: 18)
        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        dateTime.numberOfLines = 1
        dateTime.tintColor = .label
        dateTime.text = "7:03"
        dateTime.translatesAutoresizingMaskIntoConstraints = false
        lastMessage.numberOfLines = 2
        lastMessage.tintColor = .lightGray
        lastMessage.lineBreakMode = .byTruncatingTail
        lastMessage.translatesAutoresizingMaskIntoConstraints = false

        profileImageView.contentMode = .scaleAspectFill
        profileImageView.layer.cornerRadius = profileImageView.frame.size.width
        profileImageView.clipsToBounds = true
        profileImageView.translatesAutoresizingMaskIntoConstraints = false

        contentView.addSubview(profileImageView)
        contentView.addSubview(nameLabel)
        contentView.addSubview(lastMessage)
        contentView.addSubview(dateTime)
        contentView.layoutMargins = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 3)
        let lg = contentView.layoutMarginsGuide
        NSLayoutConstraint.activate([
            profileImageView.leadingAnchor.constraint(equalTo: lg.leadingAnchor),
            profileImageView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
            profileImageView.heightAnchor.constraint(equalToConstant: 50),
            profileImageView.widthAnchor.constraint(equalToConstant: 50),

            nameLabel.topAnchor.constraint(equalTo: lg.topAnchor),
            nameLabel.leadingAnchor.constraint(equalTo: profileImageView.trailingAnchor, constant: 6),
            nameLabel.trailingAnchor.constraint(equalTo: dateTime.leadingAnchor),
            nameLabel.bottomAnchor.constraint(equalTo: lastMessage.topAnchor, constant: 6),
            dateTime.topAnchor.constraint(equalTo: lg.topAnchor),
            dateTime.trailingAnchor.constraint(equalTo: lg.trailingAnchor),
            dateTime.bottomAnchor.constraint(equalTo: lastMessage.topAnchor),

            lastMessage.leadingAnchor.constraint(equalTo: profileImageView.trailingAnchor, constant: 6),
            lastMessage.trailingAnchor.constraint(equalTo: lg.trailingAnchor),
            lastMessage.bottomAnchor.constraint(equalTo: lg.bottomAnchor)
        ])
    }

这是在初始化中。谢谢!

编辑:UITableViewCell不是问题。我已将表格嵌入水平UIScrollView(repo)中,但是表格的自动布局约束是顶部,顶部,底部和底部锚点。我将其切换为除尾随锚以外的所有锚,以便可以使用宽度锚代替常数view.bounds.width

swift uitableview autolayout uilabel
2个回答
1
投票

将此添加到您的代码中:

yourLabel.numberOfLines = 0

并且不要将height constraint交给您的label。因此,它可以根据text height自动调整自身大小。

希望有帮助...


0
投票

通过此更新代码:

nameLabel.numberOfLines = 0
© www.soinside.com 2019 - 2024. All rights reserved.