如果需要更改默认的UITextField清除按钮、左右视图等等控件的位置,可以通过如下设置:
// 控制清除按钮的位置 (默认 width = 15 = height)-(CGRect)clearButtonRectForBounds:(CGRect)bounds {// NSLog(@"=%f ,=%f",bounds.origin.y,bounds.size.height/2); return CGRectMake(bounds.origin.x + bounds.size.width - 50, (bounds.size.height - 15)/2, 15, 15);}// 控制placeHolder的位置 默认(0,0,width,height)-(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); return inset;}// 控制显示文本的位置 默认(0,0,width,height)-(CGRect)textRectForBounds:(CGRect)bounds { NSLog(@"显示文本==%@",NSStringFromCGRect(bounds)); //return CGRectInset(bounds, 50, 0); CGRect inset = CGRectMake(bounds.origin.x + 190, bounds.origin.y, bounds.size.width - 10, bounds.size.height); return inset; }// 控制编辑文本的位置-(CGRect)editingRectForBounds:(CGRect)bounds { NSLog(@"编辑文本==%@",NSStringFromCGRect(bounds)); //return CGRectInset( bounds, 10 , 0 ); CGRect inset = CGRectMake(bounds.origin.x + 100, bounds.origin.y, bounds.size.width -10, bounds.size.height); return inset;}// 控制左视图位置 (默认 width=30)- (CGRect)leftViewRectForBounds:(CGRect)bounds { NSLog(@"左视图位置==%@",NSStringFromCGRect(bounds)); CGRect inset = CGRectMake(bounds.origin.x + 2, bounds.origin.y, 30, bounds.size.height); return inset; //return CGRectInset(bounds,50,0);}// 控制placeHolder的颜色、字体- (void)drawPlaceholderInRect:(CGRect)rect { [[self placeholder] drawInRect:rect withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.0f], NSForegroundColorAttributeName:[UIColor darkGrayColor] }]; }// 右视图的位置 (默认整个 width)- (CGRect)rightViewRectForBounds:(CGRect)bounds { NSLog(@"右视图位置==%@",NSStringFromCGRect(bounds)); CGRect inset = CGRectMake(bounds.size.width - 30, bounds.origin.y, 30, bounds.size.height); return inset;}