The User Defined Runtime Attributes is a feature that is not valued but is very powerful, and it can configure some properties that you cannot configure in Interface builder. When you want to do as many things as possible in IB, this feature can help you write more lightweight viewcontroller.
1. How to use
When you use Interface Builder is to click on any one of the controls to convert to the identity Inspector
Figure 1
2 Property Types
Here are some of the available attribute types with their corresponding data types
Boolean –bool
Number – NSNumber * or any numeric scalar, e.g. Nsinteger
String –nsstring *
Point–cgpoint
Size–cgsize
Rect –cgrect
Range – Nsrange
Color –uicolor *
Here are some of the following special types:
Nil-is setting this property to nil
Localized String-a key value in the locale file that will be in your strings file to match the correspondinglocalizationCharacter
Demo
Configure a view below the properties of the Calayer, generally we are in the Viewcontroller viewdidload write code to configure, now if you want to set
Layer.borderwidth and Layer.cornerradius You only need the following:
The compilation runs as follows:
If you want to configure a custom control , you can configure your custom properties in user Defined Runtime attributes as your custom button customizes a property called name that can
If you want to get your custom attributes in your code, you can
ID value = [sender valueforkey:@ "name"];
Converting from other Types
As long as the corresponding attribute type represents the same data type, then they can replace each other with each other. For example: Point and size they are actually the same struct {float a,float b}.
But in addition to the above several attribute type, you can also use other IB does not support data type? For example I want to configure Calayer border coloer and shadow color, they are all cgcolorref types and cannot be configured directly in user Defined Runtime attributes, but see the solution :
Figure 5
To be compatible with Calayer's KVC, you have to add a category to Calayer
@implementation Calayer (additions)
-(void) Setbordercolorfromuicolor: (Uicolor *) color
{
Self.bordercolor = color. Cgcolor;
}
@end
Convert from String
This is often converted from nsstring to the type you want, such as configuring a uiedgeinsets-type attribute, which cannot be supported by runtime properties, but can be considered as follows
@implementation Uiscrollview (additions)
-(void) setcontentinsetfromstring: (NSString *) contentinsetstring
{
Self.contentinset = uiedgeinsetsfromstring (contentinsetstring);
}
@end
User Defined Runtime Attributes