IB_DESIGNABLEとIBInspectable

XCodeでInterfaceBuilderようの新たなキーワードが定義された。 定義されたのはIB_DESIGNABLEとIBInspectableの2つ。

IB_DESIGNABLE 編集可能にしたいクラスに付ける
IBInspectable 編集可能にしたいプロパティに付ける

IB_DESIGNABLEは以下のように@interfaceの前に付ける。

IB_DESIGNABLE

@interface CustomView : NSView
.
.
.

IBにこのクラスが編集可能である事を知らせる為だけのキーワードらしい。

一方、IBInspectableは少し複雑。

@interface CustomView : NSView

@property (nonatomic) IBInspectable NSColor* fillColor;
@property (nonatomic) IBInspectable NSPoint point;
@property (nonatomic) IBInspectable NSRect rect;
@property (nonatomic) IBInspectable NSString* string;
@property (nonatomic) IBInspectable BOOL thisIsBool;
@property (nonatomic) IBInspectable NSImage* thisIsImage;
@property (nonatomic) IBInspectable NSInteger intgerValue;
@property (nonatomic) IBInspectable float floatValue;
@property (nonatomic) IBInspectable double doubleValue;
.
.
.

上記のように型名の前にIBOutletのように付ける。そうするとIB側が適当にインスペクターに表示してくれる。 NSImageやNSColorはiOSではUIImageやUIColorになると思われる。

../../../_images/IBInspectable.png

使える、型は決まっているようで物は使えなかった。

// Number型は表示出来ない
//@property (nonatomic) IBInspectable NSNumber* number;

// NSRangeは表示出来ない
//@property (nonatomic) IBInspectable NSRange range;

// NSValueは表示出来ない
//@property (nonatomic) IBInspectable NSValue* thisIsNSValue;

// 列挙は表示出来ない
//@property (nonatomic) IBInspectable TEST_ENUM updateType;

NSRangeが出来ないのは意外だが、IB上ではあまり使い道が無いのかもしれない。 贅沢を言えば、角度を表すUIに対応する型や、列挙型をサポートして欲しかった。

多分、”User defined Runtime Attributes”の機能をソースコードをパースして自動化出来るようにした物なんだろう。 “User defined Runtime Attributes”を表示するとリアルタイムで値が変わっている。

../../../_images/UserDefinedRuntimeAttributes.png

昔のIBPluginとは違った方向で作っているので、NSCoderとか無関係に実装出来ている。

IBでプレビューするには、framework化した上に、同じプロジェクトでコンパイルする必要がある等の面倒な面が有った。