I. Setting the color of placeholder text
Method One: Using富文本
/** Phone number Input box */@property (Weaknonatomic) iboutlet uitextfield *phonetextfield;-(void) viewdidload {[super viewdidload]; //Create a Rich text object nsmutabledictionary *attributes = [nsmutabledictionary dictionary]; //set the color of the Rich text object Attributes[nsforegroundcolorattributename] = [uicolor Whitecolor]; //set Uitextfield placeholder text self.attributedplaceholder = [ Nsattributedstring alloc] Initwithstring:@ "mobile phone number" attributes:attributes];}
Method Two: Take advantage Runtime
of the private property name and KVC
set the property
// 设置占位文字的颜色为红色(注意下面的‘self‘代表你要修改占位文字的UITextField控件)[self setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
- Note:
_placeholderLabel.textColor
It is not writable, oh, how did we get this property? See below:
// 只调用一次(自定义UITextField)+ (void)initialize { [self getIvars];}// 获取私有变量名称+ (void)getIvars { unsigned int count = 0; Ivar *ivars = class_copyIvarList([UITextField class], &count); for (int i = 0; i < count; i++) { Ivar ivar = ivars[i]; NSLog(@"%s----%s", ivar_getName(ivar), ivar_getTypeEncoding(ivar)); }}
Check the print, find out the possible property names, try to know;
- Full code: Custom
UITextField
, get to focus (edit state) when the time is white, lose focus (non-edit state) when it is gray:
#import"YCTextField.h"#import<objc/runtime.h>#define YCPLACEHOLDERTEXTCOLOR @"_placeholderlabel.textcolor"@implementationYctextfield+ (void) Initialize {[Self getivars];}Gets the private variable name + (void) Getivars {Unsignedint count =0; Ivar *ivars = Class_copyivarlist ([Uitextfield class], &count);for (int i =0; I < count; i++) {Ivar Ivar = ivars[i];NSLog (@ "%s----%s", Ivar_getname (Ivar), Ivar_gettypeencoding (Ivar)); }}- (void) Awakefromnib {Set the color of the cursorSelf. Tintcolor =self.textcolor;} //get to focus-(bool) Becomefirstresponder { //use the runtime to get the key, set the color of the placeholder text [self setValue: self.textcolor Forkeypath:ycplaceholdertextcolor]; return [super Becomefirstresponder];} //loses Focus-(bool) Resignfirstresponder { //use the runtime to get the key, set the color of the placeholder text [self setvalue:[uicolor Graycolor] forkeypath:ycplaceholdertextcolor]; return [super Resignfirstresponder];} @end
Method Three: Place the placeholder text 画
up (rewrite - (void)drawPlaceholderInRect:(CGRect)rect;
)
- (void)drawPlaceholderInRect:(CGRect)rect{ [[UIColor orangeColor] set]; [self.placeholder drawInRect:rect withFont:[UIFont systemFontOfSize:20]];}
Two. Set the cursor color
// 设置光标的颜色self.tintColor = [UIColor redColor];
Three. Set the offset of the placeholder text
- overriding
-(CGRect)placeholderRectForBounds:(CGRect)bounds;
method
-
Can be used to set the spacing between the cursor and the placeholder
// Control the position of the placeholder, the left and right 20-(cgrect) Placeholderrectforbounds: ( cgrect) bounds{//return cgrectinset (Bounds, 20, 0); cgrect inset = cgrectmake (bounds .origin.x+50, Bounds.origin.y, Bounds.size.width-10, Bounds.size .height); //better understand return inset;}
- Expansion: The system also provides a number of similar methods
- –textrectforbounds://rewrite to reset text area
- –drawtextinrect://Change the Emoji property. When overridden, calling super can be drawn by default drawing properties, so you don't have to call super if you completely override the drawing function.
- –placeholderrectforbounds://Override to reset placeholder area
- –drawplaceholderinrect://Override to change the draw placeholder property. Calling super When overridden can be drawn by default drawing properties, and you don't have to call super if you completely override the drawing function.
- –borderrectforbounds://Rewrite to reset the edge area
- –editingrectforbounds://Rewrite to reset the editing area
- –clearbuttonrectforbounds://Rewrite to reset the Clearbutton position, changing the size may cause the picture of the button to be distorted
- –leftviewrectforbounds:
- –rightviewrectforbounds:
Wen/yotrolz (author of Jane's book)
Original link: http://www.jianshu.com/p/49b4eb97f41e
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
uitextfield-Modify placeholder text and cursor color, size