设置一个UITextField
,然后再原位置设置一个leftLabel
,
最后设置leftLabel
为textField
的leftView
。
leftViewMode
为UITextFieldViewModeAlways
。
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 50, self.view.frame.size.width - 40, 50)];
textField.backgroundColor = [UIColor lightGrayColor];
textField.text = @"测试";
textField.placeholder = @"placeholder";
[self.view addSubview:textField];
UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 50, 100, 50)];
leftLabel.text = @"leftLabel";
leftLabel.backgroundColor = [UIColor redColor];
[self.view addSubview:leftLabel];
textField.leftView = leftLabel;
textField.leftViewMode = UITextFieldViewModeAlways;
显示效果如下:
如果没有设置
leftView
:没有设置leftView
如果设置
LeftViewMode
为UITextFieldViewModeWhileEditing
:非编辑状态
编辑状态
网友评论