子类化的 UILabel 不显示
UILabel subclassing a subclass does not display
我正在尝试创建自定义 UILabel
,我的第一个子类按预期工作,我进行了一些字体属性修改和更改。
@IBDesignable class LunaLbl: UILabel {
@IBInspectable dynamic open var fontSize: CGFloat = 25 {
didSet {
updateFont()
}
}
@IBInspectable dynamic open var fontName: String = Fonts.proDisplayBold.rawValue {
didSet {
updateFont()
}
}
@IBInspectable dynamic open var fontColor: UIColor = .lunaBlack {
didSet {
updateFont()
}
}
@IBInspectable dynamic open var lineSpacing: CGFloat = 1.2 {
didSet {
updateFont()
}
}
override open func draw(_ rect: CGRect) {
super.draw(rect)
updateFont()
self.adjustsFontSizeToFitWidth = false
}
func updateFont() {
let textContent = self.text
let textString = NSMutableAttributedString(string: textContent!, attributes: [
NSAttributedString.Key.font: UIFont(name: fontName, size: fontSize)!])
let textRange = NSRange(location: 0, length: textString.length)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineSpacing
textString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: textRange)
textString.addAttribute(NSAttributedString.Key.foregroundColor, value: fontColor, range: textRange)
self.attributedText = textString
}
}
现在我的第二个子类,它继承自我的第一个子类,在我的屏幕上没有任何显示。
@IBDesignable class LunaTitleLbl : LunaLbl {
override open func draw(_ rect: CGRect) {
super.draw(rect)
fontSize = 25.0
fontName = Fonts.proDisplayBold.rawValue
fontColor = .lunaBlack
updateFont()
self.adjustsFontSizeToFitWidth = false
}
}
我做同样的事情,我直接从情节提要中继承了我的新子类,但是当我使用视图层次结构调试它时没有显示任何内容,我的标签已创建并且它采用了我想要的值,但文本没有显示.
这就是我继承子类的方式
当我更改属性名称 lineSpacing 并且一切都按预期工作时,我认为问题在于我使用了相同的标签属性名称。
我正在尝试创建自定义 UILabel
,我的第一个子类按预期工作,我进行了一些字体属性修改和更改。
@IBDesignable class LunaLbl: UILabel {
@IBInspectable dynamic open var fontSize: CGFloat = 25 {
didSet {
updateFont()
}
}
@IBInspectable dynamic open var fontName: String = Fonts.proDisplayBold.rawValue {
didSet {
updateFont()
}
}
@IBInspectable dynamic open var fontColor: UIColor = .lunaBlack {
didSet {
updateFont()
}
}
@IBInspectable dynamic open var lineSpacing: CGFloat = 1.2 {
didSet {
updateFont()
}
}
override open func draw(_ rect: CGRect) {
super.draw(rect)
updateFont()
self.adjustsFontSizeToFitWidth = false
}
func updateFont() {
let textContent = self.text
let textString = NSMutableAttributedString(string: textContent!, attributes: [
NSAttributedString.Key.font: UIFont(name: fontName, size: fontSize)!])
let textRange = NSRange(location: 0, length: textString.length)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineSpacing
textString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: textRange)
textString.addAttribute(NSAttributedString.Key.foregroundColor, value: fontColor, range: textRange)
self.attributedText = textString
}
}
现在我的第二个子类,它继承自我的第一个子类,在我的屏幕上没有任何显示。
@IBDesignable class LunaTitleLbl : LunaLbl {
override open func draw(_ rect: CGRect) {
super.draw(rect)
fontSize = 25.0
fontName = Fonts.proDisplayBold.rawValue
fontColor = .lunaBlack
updateFont()
self.adjustsFontSizeToFitWidth = false
}
}
我做同样的事情,我直接从情节提要中继承了我的新子类,但是当我使用视图层次结构调试它时没有显示任何内容,我的标签已创建并且它采用了我想要的值,但文本没有显示.
这就是我继承子类的方式
当我更改属性名称 lineSpacing 并且一切都按预期工作时,我认为问题在于我使用了相同的标签属性名称。