有没有比制作覆盖 UILabel 的自定义类更简单的方式来设置 UILabel 的样式。目前我需要一堆不同字体大小和文本颜色的样式。
问问题
1559 次
1 回答
5
我不知道这是一个好习惯......但你可以这样:
extension UILabel {
@IBInspectable var myStyle: String {
set {
switch newValue {
case "style1":
self.font = UIFont.systemFontOfSize(17)
self.textColor = UIColor.blackColor()
case "style2":
self.font = UIFont.boldSystemFontOfSize(32)
self.textColor = UIColor.redColor()
default:
break
}
}
get {
return ""
}
}
}
也许,您应该使用@IBDesignable
. 因为这在 IB 中显示了更好的预览。而且,您可以使用Editor > Size To Fit
IB 中的自动布局功能。
@IBDesignable class StyledLabel: UILabel {
@IBInspectable var myStyle: String = "" {
didSet {
switch self.myStyle {
case "style1":
self.font = UIFont.systemFontOfSize(17)
self.textColor = UIColor.blackColor()
case "style2":
self.font = UIFont.boldSystemFontOfSize(32)
self.textColor = UIColor.redColor()
default:
break
}
}
}
}
于 2015-08-13T13:48:54.457 回答