如何在Swift中将长文本标签(位于UI视图中)设置为多行?

如何在Swift中将长文本标签(位于UI视图中)设置为多行?,swift,xcode,uitableview,uilabel,Swift,Xcode,Uitableview,Uilabel,我正在尝试制作一个长文本标签,它位于UI视图的内部,用于多行。我花了两个小时寻找解决方案,但我无法解决,所以我想寻求一些帮助。这是我现在得到的图像。 这是视图层次结构调试器 在表视图单元中,我将从Github获得的存储库名称放入其中。在图像中的第三个单元格中,我想将回购协议名称设为两行,因为它很长。我看到了,这听起来与我当前的问题相似,并在我的代码中实现,但它不起作用 这是我的密码。它太长了,但关键是,我已经声明了label.numberOfLines=0和label.adjustsFontSi

我正在尝试制作一个长文本标签,它位于UI视图的内部,用于多行。我花了两个小时寻找解决方案,但我无法解决,所以我想寻求一些帮助。这是我现在得到的图像。 这是视图层次结构调试器

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

这是我的密码。它太长了,但关键是,我已经声明了
label.numberOfLines=0
label.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中的视图层次调试器来清楚地查看视图在运行时实际的大小


如果未设置尾随定位点,则无法包装文本,因为它未到达视图的“末端”。

正如@Arthur已经提到的,缺少一些尾随定位点约束。此外,我建议将多行标签的垂直contentHuggingPriority设置为low(249)。repositoryNameLabel@ArtherStefan非常感谢您的回复。所以我更新了我的问题并添加了一个视图层次调试器。所以当你们说“缺少尾随锚点”时,是不是意味着我应该让标签的尾随锚点与UI视图的尾随锚点完全相同?(如果我错了,很抱歉)尝试添加约束(可能是用注释的约束无效);-)但是设置它(至少在测试中,它解决了equalToSuperview的问题,并使用容器的尾部锚点而不是前导锚点。repositoryNameLabel.trailingAnchor.constraint(equalTo:containerView.trailingAnchor,常量:5).isActive=True顺便说一句,如果您想在swift中使用自动布局约束,我强烈建议您使用SnapKit,它在我看来可读性更高