IBDESIGNABLE is available in Interface builder since Xcode 6.. We use IBDESIGNABLE to add additional properties to the view not only just the view but UILabel UIButtons and more..
By default properties are present related to views. In some case, lets say, for a UILabel you want black border with thin width but in other view you want a label with red border and some other width and like this in other labels.. You can do this by implementing code for every label in different classes.. What if the border property is already present in the Properties of UILabel.. YES , this is possible by using IBDESIGNABLE/ IBINSPECTABLE ..
Lets take an example which will explain it more clearly..
Make a class which is a subclass of UILabel. Here it is DemoLabelAddProperties.. In main.Storyboard drag a UIlabel to the view and then assign the DemoLabelAddProperties class to the UILabel ..
In your DemoLabelAddProperties.h implement the following code :
@interface DemoLabelAddProperties : UILabel{
UIColor **_borderColor;
CGFloat _width;
}
// declare the properties you want to add for UILabel
@property(nonatomic, assign) IBInspectable UIColor *borderColor;
@property(nonatomic, assign) IBInspectable CGFloat width;
In DemoLabelAddProperties.m implement this :-
- (void)setBorderColor:(UIColor *)borderColor{
_borderColor = borderColor ; // set the border color to the selected color
}
- (UIColor *)borderColor{
return _borderColor; // get the border color
}
/* we need to give width to the border */
- (void)setWidth:(CGFloat)width{
_width = width;
}
- (CGFloat)width{
return _width;
}
/* This method is called as the program builds */
- (void)layoutSubviews
{
[super layoutSubviews];
self.layer.borderColor =_borderColor.CGColor;
self.layer.borderWidth = _width;
}
Now, open your Main.Storyboard, select the UILabel and see the properties window.. You can see the changes that the UILabel has additional property i.e BORDER COLOR and WIDTH.. You can easily set the border color of the UILabel right from the Storyboard..
If you want this property to be included in any other UILabel just set its class to the DemoLabelAddProperties(here)..
And its done !!
0 Comment(s)