如何将 UI 视图中的长文本标签制作成 Swift 中的多行?

How to make long text label, which lives in UI View, to multiple lines in Swift?

我正在尝试将位于 UI 视图内的长文本标签制作成多行。找了2个小时解决方案,没解决,所以想求助一下。这是我现在得到的图像。 这是视图层次结构调试器。

在 table 视图单元格中,我输入了从 Github 获得的存储库名称。在图像的第三个单元格中,我想将回购名称设为两行,因为它很长。我看到了 ,这听起来和我当前的问题很相似,并在我的代码中实现了,但是没有用。

这是我的代码。它太长了,但重点是,我已经声明了 label.numberOfLines = 0label.adjustsFontSizeToFitWidth = true,并且我试图让我的标签可以转换为多行,但它没有用。谁能指出我在这里做错了什么?

    import UIKit

class RepositoryCell: UITableViewCell {
    
    //MARK: - Properties
    
    let userImageView: UIImageView = {
        let img = UIImageView()
        img.contentMode = .scaleAspectFit
        img.clipsToBounds = true
        img.backgroundColor = .black

        return img
    }()
    
    let containerView: UIView = {
        let view = UIView()
        view.clipsToBounds = true
        view.backgroundColor = .systemPink
        return view
    }()
    
    let userNameLabel: UILabel = {
        let label = UILabel()
        label.textColor = .black
        label.font = UIFont.boldSystemFont(ofSize: 20)
        label.numberOfLines = 0
        label.adjustsFontSizeToFitWidth = true
        return label
    }()
    
    let repositoryNameLabel: UILabel = {
        let label = UILabel()
        label.textColor = .gray
        label.font = UIFont.systemFont(ofSize: 14)
        label.numberOfLines = 0
        label.adjustsFontSizeToFitWidth = true
        return label
    }()
    
    let starNumLabel: UILabel = {
        let label = UILabel()
        label.textColor = .systemPink
        label.font = UIFont.systemFont(ofSize: 14)
        label.backgroundColor = .black
        return label
    }()
    

    
    //MARK: - Init

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        addSubview(userImageView)
        containerView.addSubview(userNameLabel)
        containerView.addSubview(repositoryNameLabel)
        addSubview(containerView)
        addSubview(starNumLabel)
        
        configureUserNameLabel()
        configureRepositoryNameLabel()
 
        configureViewConstraints()
    }
    
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        userImageView.layer.cornerRadius = userImageView.frame.height / 2
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - Helper functions
    
    func configureCellView(repository: Repository) {
        userImageView.image = UIImage(named: "001")
        userNameLabel.text = repository.userName
        repositoryNameLabel.text = repository.repositoryName
        starNumLabel.text = "⭐️\(String(describing: repository.starNum))"
    }
    
    
    func configureUserNameLabel() {
        userNameLabel.numberOfLines = 0
        userNameLabel.adjustsFontSizeToFitWidth = true
    }
    
    func configureRepositoryNameLabel() {
        repositoryNameLabel.numberOfLines = 0
        repositoryNameLabel.adjustsFontSizeToFitWidth = true
    }
    
    func configureViewConstraints() {
        setUserImageConstraints()
        setContainerViewConstraints()
        setUserNameLabelConstraints()
        setRepositoryNameLabel()
        setStarNumLabel()
    }
    
    
    func setUserImageConstraints() {
        userImageView.translatesAutoresizingMaskIntoConstraints = false
        userImageView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        userImageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10).isActive = true
        userImageView.heightAnchor.constraint(equalToConstant: 70).isActive = true
        userImageView.widthAnchor.constraint(equalTo: userImageView.heightAnchor, multiplier: 1).isActive = true
    }
    
    
    func setContainerViewConstraints() {
        containerView.translatesAutoresizingMaskIntoConstraints = false
        containerView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        containerView.topAnchor.constraint(equalTo: topAnchor, constant: 10).isActive = true
        containerView.leadingAnchor.constraint(equalTo: userImageView.trailingAnchor, constant: 10).isActive = true
        containerView.trailingAnchor.constraint(equalTo: starNumLabel.leadingAnchor, constant: -10).isActive = true
    }

    
    func setUserNameLabelConstraints() {
        userNameLabel.translatesAutoresizingMaskIntoConstraints = false
        userNameLabel.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
        userNameLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
//        userNameLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
    }

    
    func setRepositoryNameLabel() {
        repositoryNameLabel.translatesAutoresizingMaskIntoConstraints = false
        repositoryNameLabel.topAnchor.constraint(equalTo: userNameLabel.bottomAnchor).isActive = true
        repositoryNameLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
//      repositoryNameLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true

// this doesn't work...
//      repositoryNameLabel.trailingAnchor.constraint(greaterThanOrEqualTo: containerView.leadingAnchor, constant: 5).isActive = true 
    }
    
    
    func setStarNumLabel() {
        starNumLabel.translatesAutoresizingMaskIntoConstraints = false
        starNumLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        starNumLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10).isActive = true
    }

    
    
}
 

我的猜测是您的标签的尾随约束设置不正确。 您可以使用 Xcode 中的 View Hierarchy 调试器来清楚地了解您的视图在运行时的实际大小。

如果未设置尾随锚点,则无法换行文本,因为它没有到达视图的“末端”。