多行文本不适合 UILabel
Multiline text not fitting into UILabel
我有这个代码:
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 = 0
和 sizeToFit()
检查了其他评论,但这似乎不起作用。
这是目前的样子:
您需要限制标签的宽度才能使其换行。您可以:
1。设置宽度约束
NSLayoutConstraint.activate([
nameLabel.centerXAnchor.constraint(equalTo: self.view.centerXAnchor , constant: 0),
nameLabel.centerYAnchor.constraint(equalTo: self.view.centerYAnchor , constant: 0),
nameLabel.widthAnchor.constraint(equalToConstant: 100) /// here!
])
结果:
2。将前导和尾随约束设置为超级视图
NSLayoutConstraint.activate([
nameLabel.centerXAnchor.constraint(equalTo: self.view.centerXAnchor , constant: 0), /// you can delete this
nameLabel.centerYAnchor.constraint(equalTo: self.view.centerYAnchor , constant: 0),
/// here!
nameLabel.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
nameLabel.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
])
结果:
我有这个代码:
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 = 0
和 sizeToFit()
检查了其他评论,但这似乎不起作用。
这是目前的样子:
您需要限制标签的宽度才能使其换行。您可以:
1。设置宽度约束
NSLayoutConstraint.activate([
nameLabel.centerXAnchor.constraint(equalTo: self.view.centerXAnchor , constant: 0),
nameLabel.centerYAnchor.constraint(equalTo: self.view.centerYAnchor , constant: 0),
nameLabel.widthAnchor.constraint(equalToConstant: 100) /// here!
])
结果:
2。将前导和尾随约束设置为超级视图
NSLayoutConstraint.activate([
nameLabel.centerXAnchor.constraint(equalTo: self.view.centerXAnchor , constant: 0), /// you can delete this
nameLabel.centerYAnchor.constraint(equalTo: self.view.centerYAnchor , constant: 0),
/// here!
nameLabel.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
nameLabel.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
])
结果: