多行文本不适合 UILabel 1.设置宽度约束2.设置父视图的前后约束

问题描述

我有这个代码:

class ViewController: UIViewController {
    private var nameLabel = UILabel()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        updateLable()
    }

    func updateLable ()  {
        nameLabel = UILabel(frame: view.frame)
        nameLabel.text = "Michael Dell Arron Henry Mitchell duve soooooooooooooooooooooooooc"
        nameLabel.backgroundColor = .green
        nameLabel.numberOfLines = 0
        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(nameLabel)
        NSLayoutConstraint.activate([
            nameLabel.centerXAnchor.constraint(equalTo: self.view.centerXAnchor,constant: 0),nameLabel.centerYAnchor.constraint(equalTo: self.view.centerYAnchor,constant: 0)
            
        ])
        
        nameLabel.isHidden = false
        
    }

}

我无法将文本放入标签中。我用 numberOfLines = 0sizeToFit() 查看了其他评论,但这似乎不起作用。

这是目前的样子:

Label overflows into horizontal sides of screen

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)