我想以编程方式将一个带有多行文本的标签居中。我已经将它添加到以下约束中,但它只是显示在一行上!如何让它显示多行而不超出屏幕边界?
let noResults = UILabel()
noResults.text = self.emptyMessage
noResults.lineBreakMode = .byWordWrapping
noResults.minimumScaleFactor = 0.5
noResults.textAlignment = .center
noResults.font = UIFont(name: "Montserrat-Light", size: 20.0)!
noResults.numberOfLines = 0
noResults.adjustsFontSizeToFitWidth = true
noResults.sizeToFit()
noResults.textColor = UIColor.lightGray
noResults.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(noResults)
let yConstraint = NSLayoutConstraint(item: noResults, attribute: .centerY, relatedBy: .equal, toItem: noResults.superview, attribute: .centerY, multiplier: 1, constant: -(self.navigationController?.navigationBar.frame.height)!)
let xConstraint = NSLayoutConstraint(item: noResults, attribute: .centerX, relatedBy: .equal, toItem: noResults.superview, attribute: .centerX, multiplier: 1, constant: 0)
NSLayoutConstraint.activate([yConstraint, xConstraint])
最佳答案
你只需要添加一个宽度限制。