UITextField⽂本内边距
对于UITextField⾥⾯的⽂本到边框的距离,创建⼀个类继承UITextField。调⽤- (CGRect)textRectForBounds:(CGRect)bounds和-(CGRect)editingRectForBounds:(CGRect)bounds⽅法。代码如下:
//控制 placeHolder 的位置,左右缩 10
- (CGRect)textRectForBounds:(CGRect)bounds
{
return CGRectInset(bounds, 10, 0);
}
// 控制⽂本的位置,左右缩 10
- (CGRect)editingRectForBounds:(CGRect)bounds
{
return CGRectInset(bounds, 10, 0);
}
在使⽤的时候,和UITextField⼀样。
LFTextField *lFField = [[LFTextField alloc] initWithFrame:CGRectMake(50, 74, 180, 30)];
lFField.placeholder = @"请输⼊你的名字";
//⽂字居左
//字体号
lFField.font = [UIFont systemFontOfSize:15];
//输⼊框边框样式
lFField.borderStyle = UITextBorderStyleNone;
/
/输⼊框边框颜⾊
lFField.layer.borderColor = [UIColor orangeColor].CGColor;
lFField.layer.borderWidth = 0.5;
//placeholder颜⾊
UIColor *color = [UIColor redColor];
lFField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输⼊你的名字"
attributes:@{NSForegroundColorAttributeName:color}];
//输⼊框光标颜⾊
lFField.tintColor = [UIColor blackColor];
[self.view addSubview:lFField];
效果图:
//对于UITextField的禁⽤输⼊框的粘贴、拷贝、对选和全选,也要继承于UITextField。
- (BOOL)canPerformAction:(SEL)action withSender:(id)sendertextstyle
{
if (action == @selector(paste:) || action == @selector(selectAll:) || action == @selector(select:)) { return NO;
}
return NO;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。