1.创建对象
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];
2.设置属性
field.borderStyle = UITextBorderStyleRoundedRect;
3.添加到视图
[self.view addSubview:field];
CGFloat x = CGRectGetMinX(field.frame);
CGFloat y = CGRectGetMaxY(field.frame) + 100;
CGFloat width = CGRectGetWidth(field.frame);
UITextField *field2 = [[UITextField alloc] initWithFrame:CGRectMake(x, y, width, 50)];
field2.backgroundColor = [UIColor greenColor];
[self.view addSubview:field2];
4.设置输入框的提示文字
field.placeholder = @”小明咋夕阳下奔跑”;
5.设置输入框的文字颜色
field.textColor = [UIColor greenColor];
6.设置输入框的文字
field.font = [UIFont systemFontOfSize:20 weight:20];
field.text = @”青春”;
7.输入框的文字左对齐
field.textAlignment = NSTextAlignmentLeft;
8.文本框设置圆角
field.layer.masksToBounds = YES;
field.layer.cornerRadius = 30;
9.设置是否可以输入
field.enabled = YES;
10.设置键盘的样式
field.keyboardType = UIKeyboardTypeNumberPad;
field.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
field.keyboardType = UIKeyboardTypePhonePad;
field.keyboardType = UIKeyboardTypeTwitter;
field.keyboardType = UIKeyboardTypeWebSearch;
11.设置反回键的设置
field.returnKeyType = UIReturnKeyJoin;
field.returnKeyType = UIReturnKeyNext;
field.returnKeyType = UIReturnKeyYahoo;
field.returnKeyType = UIReturnKeyRoute;
12.是否在开始输入的时候,清空
field.clearsOnBeginEditing = YES;
13.设置文本为密文方式
field.secureTextEntry = YES;
14.设置辅助视图
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 230, 50)];
view1.backgroundColor = [UIColor orangeColor];
field.inputAccessoryView = view1;
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
view2.backgroundColor = [UIColor purpleColor];
field.inputView = view2;
15.键盘样式
field.borderStyle = UITextBorderStyleRoundedRect;
16-1.删除按钮始终在
field.clearButtonMode = UITextFieldViewModeAlways;
16-2.删除按钮始终没有
field.clearButtonMode = UITextFieldViewModeNever;
16-3.非编辑状态有,点进去删除按钮消失
fieldclearButtonMode = UITextFieldViewModeUnlessEditing;
16-4.编辑的时候有删除按钮
field.clearButtonMode = UITextFieldViewModeWhileEditing;
17.在文本框里添加view
UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
view3.backgroundColor = [UIColor orangeColor];
field.leftView = view3;
field.leftViewMode = UITextFieldViewModeAlways;
18.收回键盘
1)在ViewController.m中添加代理
@interface ViewController () 《UITextFieldDelegate》(此应为“< >”,因使用无法显示,以“《》”代替)
2)在 {} 设置代理
- (void)viewDidLoad {
field.delegate = self;
}
3)实现协议
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//注销第一响应
[textField resignFirstResponder];
return YES;
}