简而言之,我想创建一个 @IBInspectable
属性,让你在 Storyboards 中从下拉菜单中选择列表中的某些内容。如果有一种方法可以创建分隔符并更好地组织 IBInspectables
,我也想知道是否可能。在我的例子中,我想为电话号码创建正则表达式字符串,这样当我进入Storyboard时,我就可以直接在下拉菜单中选择“电话号码”项目,而不是输入正则表达式字符串。
目前,我已经对 TextField
进行了子类化,以便我可以添加更多的 IBInspectables
,比如正则表达式(你可以在图片中看到)。因此,目前我的子类化的 UITextField
是这样的:
@IBDesignable public class FRM_TextField: UITextField {
@IBInspectable public var regex : String?
public var isValid : Bool{
if let unwrappedRegex = regex{
let applied_regex_expression = NSRegularExpression.regularExpressionWithPattern(unwrappedRegex, options: nil, error: nil)
let numberOfMatches = applied_regex_expression?.numberOfMatchesInString(text, options: nil, range: NSMakeRange(0, countElements(text)))
if(numberOfMatches > 0 ){
return true
}else{
return false
}
}
return false
}
public required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
public override init(){
super.init();
}
public override init(frame: CGRect) {
super.init(frame: frame)
}
}