本人有若干成套学习视频, 可试看! 可试看! 可试看, 重要的事情说三遍 包含Java
, 数据结构与算法
, iOS
, 安卓
, python
, flutter
等等, 如有需要, 联系微信tsaievan
.
User Define Runtime Attribute
允许设置一些在IB
中无法设置的属性, Key Path
是要设置的属性名称, 该属性可以是在Attributes
检查器里不能设置的. Type
标识了该属性的类型, 如下:

这种操作我们很熟悉了:

那么假设我们需要在这里设置borderColor
, 发现不生效, 为什么呢?
是因为系统设置borderColor
的类型是CGColor
, 不在上表所显示的类型里面, 此时, 我们就需要使用自定义属性了.
比如, 给UIView分类里添加一个自定义属性:
@IBInspectable
public var ayBorderColor: UIColor? {
get {
return UIColor.init(cgColor: layer.borderColor ?? UIColor.clear.cgColor)
}
set {
layer.borderColor = newValue?.cgColor
}
}
这样, 我就可以在User Define Runtime Attribute
使用该属性了, 因为该属性是UIColor
类型
同样,
layer.masksToBounds
layer.cornerRadius
我们还可以简化成一个自定义属性:
@IBInspectable
public var ayCornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.masksToBounds = true
layer.cornerRadius = newValue
}
}
所以在最后使用的过程中, 就变成了这样:

方便快捷!
网友评论