Add attributes to a category
About the creation of classifications we're going to do this right here.
Here is an example of text-based color for placeholder text
One: Simple Way (Key technology: KVC)
1. Define a property in the header file
1 @property (nonatomic, strong) Uicolor * 2 * 3 * classification only produces declarations of attributes corresponding to setter and getter methods, no implementation, We need to implement the 4 */ 5 // -(void) Setplaceholdercolor: ( Uicolor *) Placeholdercolor; 6 -( Uicolor *) Placeholdercolor;
2. Define a static constant for the settings of the following properties
1 static NSString * const Placeholdercolorkey = @ " Placeholderlabel.textcolor";
3. Implement setter and Getter properties for this property (System will not be implemented in the classification)
1-(void) Setplaceholdercolor: (Uicolor *) Placeholdercolor2 {3BOOL change =NO;4 5 if(Self.placeholder = =Nil) {6Self.placeholder =@" ";7Change =YES;8 }9 Ten [self setvalue:placeholdercolor forkeypath:placeholdercolorkey]; One A if(change) { -Self.placeholder =Nil; - } the } - --(Uicolor *) Placeholdercolor - { + return[self valueforkeypath:placeholdercolorkey]; -}
Two: Loading mode (Key technology: Runtime)
1. Define a property in the header file
1 @property (nonatomic, strong) Uicolor *placeholdercolor;
2. Import the run-time library
1 #import <objc/runtime.h>
Implement Setterg and Getter methods to correlate data using management Object technology
1-(void) Setplaceholdercolor: (Uicolor *) Placeholdercolor2 {3 /**4 * Related objects: 15 */6Objc_setassociatedobject (Self, &Placeholdercolorkey, Placeholdercolor, objc_association_assign);7 }8 9-(Uicolor *) PlaceholdercolorTen { One /** A * Related objects: 2 - */ - returnObjc_getassociatedobject (Self, &placeholdercolorkey); the}
More about the operation of the relevant technology to see the author earlier related articles
iOS development--Practical Technology OC Chapter & Add attribute to category