微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Swift中将多行文本标签位于UI视图中制成多行?

如何解决如何在Swift中将多行文本标签位于UI视图中制成多行?

我正在尝试将一个长文本标签多行显示)保存在UI视图中,该文本标签位于UI视图内。我一直在寻找解决方案2个小时,但无法解决,因此想寻求帮助。这是我现在所得到的图像。

enter image description here

这是View Hierarchy调试器。

enter image description here

在表格视图单元格中,我输入了从Github获得的存储库名称。在图像的第三个单元格中,我想将存储库名称命名为两行,因为它很长。我看到了this StackOverflow question,这听起来与我当前的问题类似,并且已在我的代码中实现,但没有用。

这是我的代码。太长了,但是要点是,我已经声明了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调试器来清晰查看运行时视图的实际大小。

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。