Reputation: 188
I created a custom UILabel
and defined some styles. It only displayed the attributes on Interface Builder. Nothing changed when I built. What am I missing here?
@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)
}
}
Upvotes: 0
Views: 793
Reputation: 707
The problem is customStyle
method call before the set text on a label. so the change is the call of the customStyle
method.
@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"
Upvotes: 2