我不知道这是一个好的做法......但你可以这样做:
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 ""
}
}
}
Run Code Online (Sandbox Code Playgroud)
也许,您应该使用 定义一个子类@IBDesignable。因为这在 IB 中显示了更好的预览。并且,您可以使用Editor > Size To FitIB 中的自动布局功能。
@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
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1686 次 |
| 最近记录: |