自定义 UITextField

简单设置placeHolder字体的大小大小 颜色

NSString*placeholderString =@"搜一搜";

NSMutableAttributedString*placeholder = [[NSMutableAttributedStringalloc]initWithString:placeholderString];

[placeholderaddAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:14]range:NSMakeRange(0, placeholderString.length)];

[placeholderaddAttribute:NSForegroundColorAttributeNamevalue:[UIColoryellowColor]range:NSMakeRange(0,placeholderString.length)];

self.searchTextField.attributedPlaceholder= placeholder;



要对UItextField 的光标 leftView 以及显示文本 位置 要重写

- (CGRect)borderRectForBounds:(CGRect)bounds;

- (CGRect)textRectForBounds:(CGRect)bounds;

- (CGRect)placeholderRectForBounds:(CGRect)bounds;

- (CGRect)editingRectForBounds:(CGRect)bounds;

- (CGRect)clearButtonRectForBounds:(CGRect)bounds;

- (CGRect)leftViewRectForBounds:(CGRect)bounds;

- (CGRect)rightViewRectForBounds:(CGRect)bounds;

//控制清除按钮的位置

-(CGRect)clearButtonRectForBounds:(CGRect)bounds

{

returnCGRectMake(bounds.origin.x+ bounds.size.width-50, bounds.origin.y+ bounds.size.height-20,16,16);

}

//控制placeHolder的位置,左右缩20

-(CGRect)placeholderRectForBounds:(CGRect)bounds

{

//return CGRectInset(bounds, 20, 0);

CGRectinset =CGRectMake(bounds.origin.x+35, bounds.origin.y+2, bounds.size.width-10, bounds.size.height);//更好理解些

returninset;

}

//控制显示文本的位置

-(CGRect)textRectForBounds:(CGRect)bounds

{

//return CGRectInset(bounds, 50, 0);

CGRectinset =CGRectMake(bounds.origin.x+35, bounds.origin.y, bounds.size.width-10, bounds.size.height);//更好理解些

returninset;

}

//控制编辑文本的位置

-(CGRect)editingRectForBounds:(CGRect)bounds

{

//return CGRectInset( bounds, 10 , 0 );

CGRectinset =CGRectMake(bounds.origin.x+35, bounds.origin.y, bounds.size.width-10, bounds.size.height);

returninset;

}

//控制左视图位置

- (CGRect)leftViewRectForBounds:(CGRect)bounds

{

CGRectinset =CGRectMake(bounds.origin.x+15, bounds.origin.y+9,15,13);

returninset;

//return CGRectInset(bounds,50,0);

}

你可能感兴趣的:(自定义 UITextField)