@IBInspectable with enum?

6 Solutions Collect From Internet About “@IBInspectable with enum?”

That’s not possible (for now). You can only use those types that you see in User Defined Runtime Attributes section.

From Apple’s doc:

You can attach the IBInspectable attribute to any property in a class declaration, class extension, or category for any type that’s supported by the Interface Builder defined runtime attributes: boolean, integer or floating point number, string, localized string, rectangle, point, size, color, range, and nil.

Another work-around for this is to alter how an enumeration property appears to interface builder. For example:

#if TARGET_INTERFACE_BUILDER
@property (nonatomic, assign) IBInspectable NSInteger fontWeight;
#else
@property (nonatomic, assign) FontWeight fontWeight;
#endif

This assumes an enum called FontWeight. It relies on the fact that enums and their raw integer values can be used somewhat interchangeably in Objective-C. After doing this you are able to specify an integer in Interface builder for the property which is not ideal, but works, and retains a small amount of type safety when using the same property programatically.

This is a better alternative than declaring a separate integer property because you don’t need to write extra logic to handle a second integer property which could also be used to accomplish the same thing.

However, this does not work with Swift because we’re not able to implicitly cast from an integer to an enum. Any thoughts on solving that would be appreciated.

I do this using a Inspectable NSInteger value and override the setter to allow it to set the enum. This has the limitation of not using a popup list and if you change your enum values, then the interface options will not update to match.

Example.

In Header File:

typedef NS_ENUM(NSInteger, LabelStyle)
{
    LabelStyleContent = 0, //Default to content label
    LabelStyleHeader,
};

...

@property LabelStyle labelStyle;
@property (nonatomic, setter=setLabelAsInt:) IBInspectable NSInteger labelStyleLink;

In the implementation file:

- (void)setLabelAsInt:(NSInteger)value
{
    self.labelStyle = (LabelStyle)value;
}

You could optionally add some logic in there to ensure that it is being set to a valid value

As @sikhapol answered, this is not possible. The workaround I use for this is to have a bunch of IBInspectable bools in my class and just select one in interface builder. For added security that multiple ones are not set, add an NSAssert in the setter for each one.

- (void)setSomeBool:(BOOL)flag
{
    if (flag)
    {
        NSAssert(!_someOtherFlag && !_someThirdFlag, @"Only one flag can be set");
    }
}

This is a little tedious and a bit sloppy IMO, but it’s the only way to accomplish this kind of behavior that I can think of

I want to add that the identifiers of an enum are not available at runtime for anybody in Objective-C. So there cannot be a possibility to display it anywhere.

My solution was to do :

@IBInspectable  
var keyboardType = UIKeyboardType.default.rawValue {
        didSet { 
             textField.keyboardType = UIKeyboardType(rawValue: keyboardType)! 
        }
}

On the IB itself, you will need to set an int in the keyboardType field