36

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.

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:

@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)
  }   
}

Regex Toolbar

3
  • 1
    For people coming here later, you could improvise/make do with a few BOOL properties that the user can toggle on and off. It's not ideal but it's an option.
    – hhanesand
    Aug 21, 2015 at 1:24
  • @Jordan Hochstetler +1 for your question did you got the solution for this ? Jan 3, 2018 at 11:11
  • @Rakesh still no answer for an array of options but a few of the answers below help with organization Mar 29, 2019 at 16:18

3 Answers 3

16

There is no support of any lists or arrays yet.

Currently the following types support @IBInspectable

  • Int
  • CGFloat
  • Double
  • String
  • Bool
  • CGPoint
  • CGSize
  • CGRect
  • UIColor
  • UIImage

Here is a code with all available IBInspectable's:

    @IBInspectable var integer: NSInteger = 10
    @IBInspectable var float: CGFloat = 10
    @IBInspectable var double: Double = 10
    @IBInspectable var string: String = "string"
    @IBInspectable var bool: Bool = true
    @IBInspectable var point: CGPoint = CGPointMake(1, 0)
    @IBInspectable var rect: CGRect = CGRectMake(0, 0, 100, 100)
    @IBInspectable var color: UIColor = UIColor.redColor()
    @IBInspectable var size: CGSize = CGSizeMake(100, 100)
    @IBInspectable var image: UIImage = UIImage(named: "Logo")!

And it looks in IB like this:

enter image description here

1
  • Apple documentation on supported types: You can add the IBInspectable attribute to any property in a class declaration, class extension, or category of type: boolean, integer or floating point number, string, localized string, rectangle, point, size, color, range, and nil.
    – Adam Johns
    Nov 24, 2016 at 1:52
12

As far as organization, You can organize it with dividers by naming your properties so that they have the same prefix.

@IBInspectable var ValText : Bool! = false
@IBInspectable var ValEmail : Bool! = false
@IBInspectable var ValCreditCard : Bool! = false
@IBInspectable var Positives : Bool! = false
@IBInspectable var Money : Bool! = false
@IBInspectable var Phone : Bool! = false
@IBInspectable var ZipCode : Bool! = false
@IBInspectable var Street : Bool! = false
@IBInspectable var IPAddress : Bool! = false
@IBInspectable var MAC : Bool! = false
@IBInspectable var AlphaNum : Bool! = false
@IBInspectable var AlphaNumSpaces : Bool! = false
@IBInspectable var AlphaNumNoSpaces : Bool! = false
@IBInspectable var URL : Bool! = false
@IBInspectable var ValidationType : String! = ""

Renders as

IB

10

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.

As far as I know, lists (arrays) are not supported yet. Suported types so far are:

  • Int
  • CGFloat
  • Double
  • String
  • Bool
  • CGPoint
  • CGSize
  • CGRect
  • UIColor
  • UIImage

Also if there is a way to create dividers and better organize the IBInspectables.

I don't think that such thing is possible. But maybe someone has a workaround.

I recommend you to watch WWDC Session 411 - What's New in Interface Builder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.