1、利用UITextField的属性leftView,创建一个宽为5,高为20的View设置为UITextField的leftView,这样视觉上就会有5像素的左边距
[Objective-C] 纯文本查看 复制代码 UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;
2、MAC OS中以及iOS7.0以前可以使用如下方法设置
[Objective-C] 纯文本查看 复制代码 [self.yourTextField setValue:[NSNumber numberWithInt:5] forKey:@"_paddingTop"];
[self.yourTextField setValue:[NSNumber numberWithInt:5] forKey:@"_paddingLeft"];
[self.yourTextField setValue:[NSNumber numberWithInt:5] forKey:@"_paddingBottom"];
[self.yourTextField setValue:[NSNumber numberWithInt:5] forKey:@"_paddingRight"];
3、自定义一个TextField,实现如下两个方法
[Objective-C] 纯文本查看 复制代码 //控制placeHolder 的位置,左右缩 8px
- (CGRect)textRectForBounds:(CGRect)bounds
{
return CGRectInset(bounds , 8 , 0 );
}
// 控制文本的位置,左右缩 8px
- (CGRect)editingRectForBounds:(CGRect)bounds
{
return CGRectInset(bounds , 8 , 0 );
}
|