作者:Rianbow_小渊渊设 | 来源:互联网 | 2023-08-31 16:06
Inshort,Iwouldliketocreatean@IBInspectablepropertythatallowsyoutoselectfromalistof
In short, I would like to create an @IBInspectable
property that allows you to select from a list of things in a drop down menu when you are in Storyboards. Also if there is a way to create dividers and better organize the IBInspectables
I would like to know if this is possible too. In my example, I would like to create regex strings for a phone number so that when I go to the storyboard I can just select the "phone number" item in a drop down menu instead of entering a regex string.
简而言之,我想创建一个@IBInspectable属性,允许您在Storyboard中从下拉菜单中选择一些内容。此外,如果有办法创建分隔线并更好地组织IBInspectables我想知道这是否也可行。在我的示例中,我想为电话号码创建正则表达式字符串,以便当我转到故事板时,我可以在下拉菜单中选择“电话号码”项,而不是输入正则表达式字符串。
Currently I have subclassed a TextField
so that I can add even more IBInspectables
to it like regex (which you can see in the picture). So as it stands this is what I have for my subclassed UITextField
:
目前我已经将一个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)
}
}
3 个解决方案