对于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.textAlignment = NSTextAlignmentLeft;
//字体号
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)sender
{
if (action == @selector(paste:) || action == @selector(selectAll:) || action == @selector(select:)) {
return NO;
}
return NO;
}