Http://www.tuicool.com/articles/JVNRBjY
@IBDesignable and @ibinspectabletime 2014-10-08 11:02:03 csdn Blog Original http://blog.csdn.net/tounaobun/article/details/39890667 ThemeInterface Builderobjective-c
I've been looking at two of the swift official tutorials from Apple, one is the swift programming Language and the other is the Using Swift with Cocoa and Objective-c. Yesterday happened to see the "Writing Swift Classes with objective-c Behavior" section of the second ebook, which describes the technology for real-time rendering. Here is a excerpt from one of the sections:
"Live renderingyou can use different attributes-@IBDesignable and @IBInspectable-to enable live, interactive custom VI EW Design in Interface Builder. When youCreate a customView that inheritsFrom the UIView classOr the NSView class, you canAdd the @IBDesignable attribute just before the class declaration. After youAdd the CustomViewTo Interface Builder (By setting the custom classof theViewIn the Inspector pane), Interface Builder renders yourViewIn the canvas. You can alsoadd the @IBInspectable attribute to properties With types compatible with user defined runtime attributes. After you add your custom view to Interface Builder, You can edit these properties in the inspector. Swift@ibdesignableclass Mycustomview:uiview { @IBInspectable var textcolor:uicolor @IBInspectable var iconheight:cgfloat/*/}" excerpt from: Apple Inc " Using Swift with Cocoa and objective-c ". IBooks. Https://itunes.apple.com/cn/book/using-swift-cocoa-objective/id888894773?mt= 11
The idea is that you can render your custom code in real time to interface builder. And the bridge between them is done by two instructions, namely @ibdesignable and @ibinspectable. We tell interface Builder through @ibdesignable that this class can be rendered to the interface in real time, but this class must be a subclass of UIView or NSView. By @ibinspectable, you can define dynamic properties to visualize and modify property values in the Attribute Inspector panel.
Not much to say, here's a simple example, this example customizes a UIView subclass that has a uibutton.
Import uikit@ibdesignableClass Mycustomview:uiview { @IBInspectablevar buttontitlecolor:uicolor!Button title Color@IBInspectablevar buttontitle:string!Button title@IBInspectablevar buttonframe:cgrect!Button frame var mybutton:uibutton! Override Init (Frame:cgrect) { Init stored Properties Buttontitlecolor = Uicolor.redcolor () Buttontitle ="Button title" Buttonframe = CGRectMake (0,0,100,50) MyButton = UIButton (frame:buttonframe) Mybutton.settitlecolor (Buttontitlecolor, forstate:. Normal) Mybutton.settitle (Buttontitle, forstate:. Normal) Call Super initializer Super.init (Frame:frame) Add button to self Addsubview (MyButton)} Required Init (coder Adecoder:nscoder) { Init stored Properties Buttontitlecolor = Uicolor.redcolor () Buttontitle ="Button title" Buttonframe = CGRectMake (0,0,100,50) MyButton = UIButton (frame:buttonframe) Mybutton.settitlecolor (Buttontitlecolor, forstate:. Normal) Mybutton.settitle (Buttontitle, forstate:. Normal) //call super Initializer Span class= "indent" > Super.init (coder:adecoder) Span class= "indent" > //add button to Self Addsubview (MyButton) } override func Layoutsubviews () { //refresh button state through Attribute Inspector Mybutton.settitlecolor (Buttontitlecolor, forstate:. Normal) Mybutton.settitle (Buttontitle, forstate:. Normal) }}
:
As you can see, the code renders to IB in real time.
Also, we can see in the attribute inspector that the attributes declared by the directive @ibinspectable also appear in the panel, which can be changed dynamically by modifying the values of the interface (the Layoutsubviews method needs to be implemented)
Specific projects at a glance:
IOS @IBDesignable and @ibinspectable