gpt4 book ai didi

ios - 无法在 UILabel、Swift 中换行文本

转载 作者:行者123 更新时间:2023-11-30 12:14:55 24 4
gpt4 key购买 nike

我见过几个这样的问题,但没有太多帮助。

我正在 View Controller 中使用自定义类创建一个带有边缘插入的 UILabel,如下所示 var message = InsetLabel()。在 viewDidLoad 中,我正在自定义标签以包裹文本,但它似乎不起作用。我哪里出错了?

我没有得到全文,如下图所示:

enter image description here

class InsetLabel: UILabel {
override func drawText(in rect: CGRect) {
super.drawText(in: UIEdgeInsetsInsetRect(rect, UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)))
}
}


class ViewController: UIViewController {
var messageHeading = UILabel()
var message = InsetLabel()
var messageText = "Your profile is currently going through the approval process, You will be notified as soon as it has been appproved."

override func viewDidLoad() {
super.viewDidLoad()

messageHeading.text = "APPROVAL STATUS"
messageHeading.textColor = Colors.darkGrey
messageHeading.textAlignment = .left

message.text = messageText
message.textColor = Colors.darkGrey
message.textAlignment = .left
message.lineBreakMode = .byWordWrapping
message.numberOfLines = 10
message.backgroundColor = Colors.white
message.sizeToFit()
view.addSubview(messageHeading)
view.addSubview(message)

messageHeading.translatesAutoresizingMaskIntoConstraints = false
message.translatesAutoresizingMaskIntoConstraints = false

let views = ["messageHeading" : messageHeading,
"message" : message
]

view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[messageHeading]", options: [], metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[messageHeading]-10-|", options: [], metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[messageHeading]-10-[message]", options: [], metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[message]|", options: [], metrics: nil, views: views))
}
}

最佳答案

更改您的自定义标签类别

class InsetLabel: UILabel {
override func drawText(in rect: CGRect) {
super.drawText(in: rect)
// super.drawText(in: UIEdgeInsetsInsetRect(rect, UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)))
}
}

然后添加如下约束:

  let msgHeadVerticalConst =  NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[messageHeading(>=20)]", options: [], metrics: nil, views: views)
let msgHeadHorizontalConst = NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[messageHeading]-10-|", options: [], metrics: nil, views: views)

let msgVerticalConst = NSLayoutConstraint.constraints(withVisualFormat: "V:[messageHeading]-10-[message(>=20)]", options: [], metrics: nil, views: views)

let msgHorizontalConst = NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[message]-10-|", options: [], metrics: nil, views: views)

NSLayoutConstraint.activate(msgHeadVerticalConst + msgHeadHorizontalConst + msgVerticalConst + msgHorizontalConst)

关于ios - 无法在 UILabel、Swift 中换行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45557681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com