本人新手,最近在工作中遇到一个需求,也是大家也很常见的UITextField输入框的问题。
需求如下:
公司之前的处理方式在父view上面添加UILabel、UITextField,以及分割线,这样控件比较多,而且每次都要计算控件的Frame也麻烦,然后我就直接对UITextField进行了一点小封装,将UILabel和分割线直接添加UITextField。废话少说,直接上代码。
- (instancetype)initWithFrame:(CGRect)frame withLabelTitle:(NSString *)title withPlaceholder:(NSString *)holder
{
self = [super initWithFrame:frame];
if (self) {
//设置占位符的内容和颜色
self.placeholder = holder;
[self setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 12, 100, 20)];
titleLabel.text = title;
titleLabel.textColor = [UIColor blackColor];
titleLabel.font = [UIFont systemFontOfSize:16];
[self addSubview:titleLabel];
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10, 43, 375 - 20, 1)];
lineView.backgroundColor = [UIColor darkTextColor];
[self addSubview:lineView];
self.backgroundColor = [UIColor whiteColor];
self.textColor = [UIColor blackColor];
self.font = [UIFont systemFontOfSize:16];
}
return self;
}
这时候,你主要传两个参数进去就可以了,一个是UITextField的主题,一个是UITextField的占位符。但是这样的话,占位符会覆盖主题,编辑光标也是从最左边开始,所以需要设置他们的初始位置。下面几个办法就是对UITextField几个属性的位置进行重置。
//设置占位符的位置
-(CGRect)placeholderRectForBounds:(CGRect)bounds
{
CGFloat f = [self getWidthFromString];
CGRect inset = CGRectMake(bounds.origin.x+f + 20, bounds.origin.y, bounds.size.width -10, bounds.size.height);//更好理解些
return inset;
}
//设置编辑光标的位置
-(CGRect)editingRectForBounds:(CGRect)bounds
{
CGFloat f = [self getWidthFromString];
CGRect inset = CGRectMake(bounds.origin.x + f + 20, bounds.origin.y, bounds.size.width -10, bounds.size.height);
return inset;
}
//设置文本内容的位置
-(CGRect)textRectForBounds:(CGRect)bounds
{
CGFloat f = [self getWidthFromString];
CGRect inset = CGRectMake(bounds.origin.x + f + 20, bounds.origin.y, bounds.size.width -10, bounds.size.height);
return inset;
}
//设置清除按钮的位置
-(CGRect)clearButtonRectForBounds:(CGRect)bounds
{
return CGRectMake(bounds.origin.x + bounds.size.width - 50, bounds.origin.y + bounds.size.height -20, 16, 16);
}
//计算光标及占位符需要移动的
-(CGFloat)getWidthFromString
{
NSString *text = @"四个中文";
NSMutableDictionary *attrsDict = [NSMutableDictionary dictionary];
attrsDict[NSFontAttributeName] = [UIFont systemFontOfSize:16];
CGSize size = CGSizeMake(MAXFLOAT, MAXFLOAT);
return [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attrsDict context:nil].size.width;
}
以上几个方法就可以满足需求了,下面几个也可以在需要的时候设置
- (CGRect)borderRectForBounds:(CGRect)bounds
{
return CGRectMake(bounds.origin.x + bounds.size.width - 50, bounds.origin.y + bounds.size.height -20, 16, 16);
}
//设置右视图的位置
- (CGRect)rightViewRectForBounds:(CGRect)bounds
{
return CGRectMake(bounds.origin.x + bounds.size.width - 50, bounds.origin.y + bounds.size.height -20, 16, 16);
}
//设置左视图的位置
- (CGRect)leftViewRectForBounds:(CGRect)bounds
{
return CGRectMake(bounds.origin.x +10, bounds.origin.y, bounds.size.width-250, bounds.size.height);
}
//设置占位符的属性,这个已经在init初始化方法里面设置过了
- (void)drawPlaceholderInRect:(CGRect)rect
{
}
//设置文本的属性,这个已经在init初始化方法里面设置过了
- (void)drawTextInRect:(CGRect)rect
{
}
使用:
MJMCustomTextField *nameField = [[MJMCustomTextField alloc] initWithFrame:CGRectMake(0, 20, MGE_ScreenWidth, 44) withLabelTitle:@"姓 名" withPlaceholder:@"请输入你的真实姓名"];
第一次写简书,请大家多多指教,多提建议,大家共同进步。大家好才是真好,简书真好。