Objective
In our usual development, sometimes encounter background darker interface needs to add Textfield,textfield the default placeholder color is also dark gray, more difficult to see, this time need to modify the booth text color, but the system does not provide the corresponding method, then we need to customize the
Modified effect
Words not much said, the following code:
. h file
#import <UIKit/UIKit.h>
@interface uitextfield (placeholder)
@property Uicolor * Placeholdercolor;
@end
. m file #import "uitextfield+placeholder.h" #import <objc/message.h> nsstring * Const PLACEHOLDERCOLORNAME = @ "P
Laceholdercolor "; @implementation Uitextfield (Placeholder) + (void) load {//Get Setplaceholder method Setplaceholder = Class_getinstancem
Ethod (Self, @selector (setplaceholder:));
Get Bs_setplaceholder Method Bs_setplaceholder = Class_getinstancemethod (self, @selector (bs_setplaceholder:));
Exchange Method Method_exchangeimplementations (Setplaceholder, Bs_setplaceholder);
}//OC like lazy load, use the time to load//need to add properties to the system Uitextfield, only use runtime-(void) Setplaceholdercolor: (Uicolor *) Placeholdercolor { Set Association Objc_setassociatedobject (self, (__bridge const void *) (placeholdercolorname), Placeholdercolor, objc_
Association_retain_nonatomic);
Set placeholder text color Uilabel *placeholderlabel = [self valueforkeypath:@ "Placeholderlabel"];
Placeholderlabel.textcolor = Placeholdercolor; }-(Uicolor *) Placeholdercolor {//Returns association return Objc_getassociatedobject (self, __bridge const void *) (placeholdercolorname)); //Set placeholder text and set placeholder text color-(void) Bs_setplaceholder: (NSString *) placeholder {//1. Set placeholder text [self Bs_setplaceholder:placehol
Der];
2. Set placeholder text color self.placeholdercolor = Self.placeholdercolor; } @end
Summarize
The above is iOS use runtime to modify text box placeholder text color All content, hope to develop iOS can help you, if there is doubt you can message exchange.