Я создал пользовательский UILabel и определил несколько стилей. Он отображал только атрибуты в Interface Builder. Когда я строил, ничего не изменилось. Что мне здесь не хватает?
@objc public enum CustomLabelStyle: Int {
case ScreenTitle = 0
case H2 = 1
case H3 = 2
case BodyText = 3
case BodyTextGray = 4
case DescriptionText = 5
}
@IBDesignable class JWLabel: UILabel {
@IBInspectable open var style: Int = 0 {
didSet {
customStyle(style: style)
}
}
func customStyle(style: Int){
var font = UIFont(name: "HelveticaNeue-Bold", size: 12)
var color = UIColor(hexString: "#8E8E8E")
var paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 3
switch style {
case CustomLabelStyle.ScreenTitle.rawValue:
font = UIFont(name: "HelveticaNeue", size: 18)
color = UIColor(hexString: "#404040")
break
case CustomLabelStyle.H2.rawValue:
font = UIFont(name: "HelveticaNeue-Bold", size: 20)
color = UIColor(hexString: "#404040")
paragraphStyle.lineSpacing = 5
break
case CustomLabelStyle.H3.rawValue:
font = UIFont(name: "HelveticaNeue-Bold", size: 16)
color = UIColor(hexString: "#404040")
paragraphStyle.lineSpacing = 4
case CustomLabelStyle.BodyText.rawValue:
font = UIFont(name: "HelveticaNeue-Bold", size: 13)
color = UIColor(hexString: "#404040")
paragraphStyle.lineSpacing = 3
break
case CustomLabelStyle.BodyTextGray.rawValue:
font = UIFont(name: "HelveticaNeue-Bold", size: 13)
color = UIColor(hexString: "#8E8E8E")
paragraphStyle.lineSpacing = 3
break
case CustomLabelStyle.DescriptionText.rawValue:
font = UIFont(name: "HelveticaNeue-Bold", size: 12)
color = UIColor(hexString: "#8E8E8E")
paragraphStyle.lineSpacing = 2
break
default:
break
}
let attribute = [NSAttributedString.Key.font : font,
NSAttributedString.Key.foregroundColor : color,
NSAttributedString.Key.paragraphStyle : paragraphStyle]
self.attributedText = NSAttributedString(string: self.text ?? "", attributes: attribute)
}
}





Проблема в том, что вызов метода customStyle перед установленным текстом на этикетке. поэтому изменение — это вызов метода customStyle.
@IBDesignable class JWLabel: UILabel {
@IBInspectable open var style: Int = 0
func customStyle(style: Int){
var font = UIFont(name: "HelveticaNeue-Bold", size: 12)
var color = UIColor(hexString: "#8E8E8E")
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 3
switch style {
case CustomLabelStyle.ScreenTitle.rawValue:
font = UIFont(name: "HelveticaNeue", size: 18)
color = UIColor(hexString: "#404040")
break
case CustomLabelStyle.H2.rawValue:
font = UIFont(name: "HelveticaNeue-Bold", size: 20)
color = UIColor(hexString: "#404040")
paragraphStyle.lineSpacing = 5
break
case CustomLabelStyle.H3.rawValue:
font = UIFont(name: "HelveticaNeue-Bold", size: 16)
color = UIColor(hexString: "#404040")
paragraphStyle.lineSpacing = 4
case CustomLabelStyle.BodyText.rawValue:
font = UIFont(name: "HelveticaNeue-Bold", size: 13)
color = UIColor(hexString: "#404040")
paragraphStyle.lineSpacing = 3
break
case CustomLabelStyle.BodyTextGray.rawValue:
font = UIFont(name: "HelveticaNeue-Bold", size: 13)
color = UIColor(hexString: "#8E8E8E")
paragraphStyle.lineSpacing = 3
break
case CustomLabelStyle.DescriptionText.rawValue:
font = UIFont(name: "HelveticaNeue-Bold", size: 12)
color = UIColor(hexString: "#8E8E8E")
paragraphStyle.lineSpacing = 2
break
default:
break
}
let attribute = [NSAttributedString.Key.font : font,
NSAttributedString.Key.foregroundColor : color,
NSAttributedString.Key.paragraphStyle : paragraphStyle]
self.attributedText = NSAttributedString(string: self.text ?? "", attributes: attribute)
}
override func layoutSubviews() {
super.layoutSubviews()
self.customStyle(style: style)
}
}
//Set text on a label
lblTest.text = "This is the dummy text for test"
У меня есть вопрос. Я делаю функцию customStyle в layoutSubviews и didSet экземпляра для рендеринга IB. Это плохой спектакль?
Вы должны установить свой собственный стиль при изменении текста. Прямо сейчас этот пользовательский стиль применяется к тексту, когда UILabel инициализируется и сначала вызывается
didSetforstyle. В этот момент текст пуст, поэтому он ничего не показывает. Когда вы устанавливаете свой текст, он не получает стиля.