gpt4 book ai didi

ios - 覆盖 UILabel 字体会导致标签被截断

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

我正在创建自定义 UILabel 类。原因是因为我想用 Constants 类来调整 Label 的属性。一旦应用程序的主要颜色发生变化,在 IB 中修改属性会变得很麻烦。不管怎样,这是我的自定义 UILabel 类:

@IBDesignable class FormTitleLabel: UILabel {

override var font: UIFont! {
get {
return UIFont.systemFont(ofSize: 36, weight: .heavy)
} set {
super.font = font
}
}
}

这会导致标 checkout 现截断:

enter image description here

我可以使用以下代码解决这个问题:

@IBDesignable class FormTitleLabel: UILabel {

override var font: UIFont! {
get {
return UIFont.systemFont(ofSize: 36, weight: .heavy)
} set {
super.font = font
}
}

override init(frame: CGRect) {
super.init(frame: frame)

setup()
}

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!

setup()
}

override func awakeFromNib() {
super.awakeFromNib()

setup()
}

private func setup() {
self.font = UIFont.systemFont(ofSize: 36)
}

}

为什么这个解决方案有效?

最佳答案

这段代码是完全错误的:

override var font: UIFont! {
get {
return UIFont.systemFont(ofSize: 36, weight: .heavy)
} set {
super.font = font
}
}

您总是返回字体 A,但在内部设置字体 B。查看绘图函数检查标签的字体以绘制文本,它们使用字体 A,尽管实际上它们应该使用字体 B。这就是为什么你有这种奇怪的行为。

关于ios - 覆盖 UILabel 字体会导致标签被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48460802/

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