UITextField文本字段控件的位置

时间:2023-03-08 16:30:23

如果需要更改默认的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 - , (bounds.size.height - )/, , );
} // 控制placeHolder的位置 默认(0,0,width,height)
-(CGRect)placeholderRectForBounds:(CGRect)bounds { // return CGRectInset(bounds, 20, 0);
CGRect inset = CGRectMake(bounds.origin.x + , bounds.origin.y, bounds.size.width - , 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 + , bounds.origin.y, bounds.size.width - , bounds.size.height);
return inset; } // 控制编辑文本的位置
-(CGRect)editingRectForBounds:(CGRect)bounds { NSLog(@"编辑文本==%@",NSStringFromCGRect(bounds));
//return CGRectInset( bounds, 10 , 0 );
CGRect inset = CGRectMake(bounds.origin.x + , bounds.origin.y, bounds.size.width -, bounds.size.height);
return inset;
} // 控制左视图位置 (默认 width=30)
- (CGRect)leftViewRectForBounds:(CGRect)bounds { NSLog(@"左视图位置==%@",NSStringFromCGRect(bounds)); CGRect inset = CGRectMake(bounds.origin.x + , bounds.origin.y, , 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 - , bounds.origin.y, , bounds.size.height);
return inset;
}