-
直接设置圆角不做其它处理是这样的:
-
然而设计图上是这样的:
-
现在简单封装了一下,效果是这样的:
-
代码简单明了、可读性高,是这样的:
注:此控件继承自UITextField
#pragma mark - 构造方法
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
// TextField的左视图
UIView *leftView = [[UIView alloc]init];
[self addSubview:leftView];
[leftView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_offset(0);
make.left.mas_offset(0);
make.bottom.mas_offset(0);
make.width.equalTo(self.mas_height).multipliedBy(0.5);
}];
// 属性设置
self.backgroundColor = [UIColor whiteColor];
self.layer.borderColor = [UIColor blackColor].CGColor;
self.layer.borderWidth = 1;
self.leftViewMode = UITextFieldViewModeAlways;
self.leftView = leftView;
}
return self;
}
#pragma mark - 调整UI
- (void)layoutSubviews{
[super layoutSubviews];
// 调整圆角大小
self.layer.cornerRadius = self.frame.size.height / 2;
}
-
使用方便,是这样的:
上面效果图的代码
LoginViewCustomTextField *textField1 = [[LoginViewCustomTextField alloc]initWithFrame:CGRectMake(90, 90, 200, 40)];
[self.view addSubview:textField1];
LoginViewCustomTextField *textField2 = [[LoginViewCustomTextField alloc]initWithFrame:CGRectMake(90, 150, 80, 20)];
[self.view addSubview:textField2];
LoginViewCustomTextField *textField3 = [[LoginViewCustomTextField alloc]init];
[self.view addSubview:textField3];
[textField3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_offset(90);
make.top.mas_equalTo(textField2.mas_bottom).mas_offset(20);
make.right.mas_offset(-90);
make.height.mas_equalTo(60);
}];
网友评论