(1)初始化UITextField
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
UITextField* text = [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 300, 30)];
text.borderStyle = UITextBorderStyleRoundedRect;
text.autocorrectionType = UITextAutocorrectionTypeYes;
text.placeholder = @
"您好,我是Andy—清风"
;
text.returnKeyType = UIReturnKeyDone;
text.clearButtonMode = UITextFieldViewModeWhileEditing;
[text setBackgroundColor:[UIColor whiteColor]];
text.delegate = self;
[self.view addSubview:text];
|
borderStyle:文本框的边框风格
autocorrectionType:可以设置是否启动自动提醒更正功能。
placeholder:设置默认的文本显示
returnKeyType:设置键盘完成的按钮
backgroundColor:设置背景颜色
delegate:设置委托
(3)委托方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
-(
void
)textFieldDidBeginEditing:(UITextField *)textField;
//当开始点击textField会调用的方法
-(
void
)textFieldDidEndEditing:(UITextField *)textField;
//当textField编辑结束时调用的方法
//按下Done按钮的调用方法,我们让键盘消失
-(
BOOL
)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return
YES;
}
|