@property (nonatomic, strong)UITextField * textField;
@property (nonatomic, strong)UIView * tfLeftView;
@property (nonatomic, strong)UIView * tfRightView;
效果如图
自定义输入框左侧是视图
-(UIView *)tfLeftView{
if (!_tfLeftView) {
_tfLeftView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 50, 44)];
label.text = @"+86 >";
label.font = [UIFont systemFontOfSize:15];
label.textColor = [UIColor colorWithRed:156.0f/255.0f green:159.0f/255.0f blue:163.0f/255.0f alpha:1];
[_tfLeftView addSubview:label];
}
return _tfLeftView;
};
自定义输入框右侧视图
-(UIView *)tfRightView{
if (!_tfRightView) {
_tfRightView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor yellowColor];
label.text = @"test";
label.font = [UIFont systemFontOfSize:15];
label.textColor = [UIColor blueColor];
[_tfRightView addSubview:label];
}
return _tfRightView;
};
-
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];self.textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 200, 300, 44)];
self.textField.placeholder = @“请输入手机号”;
self.textField.font = [UIFont systemFontOfSize:15];
self.textField.textColor = [UIColor colorWithRed:156.0f/255.0f green:159.0f/255.0f blue:163.0f/255.0f alpha:1];
self.textField.backgroundColor = [UIColor whiteColor];
self.textField.layer.cornerRadius = __kNewSize(4.f);
self.textField.layer.borderWidth = 0.5f;
self.textField.layer.borderColor =[UIColor colorWithRed:156.0f/255.0f green:159.0f/255.0f blue:163.0f/255.0f alpha:1].CGColor;self.textField.leftView = self.tfLeftView;
self.textField.leftViewMode = UITextFieldViewModeAlways;self.textField.rightView = self.tfRightView;
self.textField.rightViewMode = UITextFieldViewModeAlways;[self.view addSubview:self.textField];
}