![](/img/trans.png)
[英]How to modify font properties if the font is “cut off” at the top or bottom in a UILabel
[英]Overriding UILabel font causes cut off label
我正在創建一個自定義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
}
}
}
這將導致標簽顯示為截止:
我可以使用以下代碼解決此問題:
@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
這就是為什么您會有這種奇怪的行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.