一: 创建:
UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 200, 50)];
二: UITextField的一些属性
(1) 颜色:tf.backgroundColor = [UIColor yellowColor];
(2) 文本:(可读可写): tf.text = @"dve";
(3) 占位:tf.placeholder = @"请输入用户名";
(4) 文本颜色:tf.textColor = [UIColor blueColor];
(5) 文本位置:tf.textAlignment = NSTextAlignmentCenter;
(6) 文本字体:tf.font = [UIFont fontWithName:@"HelveticaBold" size:20];
(7) 输入控制 控制能否输入 (默认是YES 可以输入):tf.enabled = NO;
(8) 是否清除上次的输入:tf.clearsOnBeginEditing = YES;
(9) 安全输入(密码模式):tf.secureTextEntry = YES;
(10) 键盘类型:tf.keyboardType = UIKeyboardTypeNumberPad;
(11) 改变return键返回的内容:tf.returnKeyType = UIReturnKeyDone;
(12) 外观控制 边框:tf.borderStyle = 3;
(13) 清除类型:tf.clearButtonMode = UITextFieldViewModeAlways;
(14) 左视图:tf.leftViewMode = UITextFieldViewModeWhileEditing;
三:用UITextField、UILabel和UIButton简单实现下面的界面
实现代码:
// 添加用户名label
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(30, 100, 80, 30)];
label1.text = @"用户名";
label1.textColor = [UIColor blackColor];
[_window addSubview:label1];
// 添加用户名textfield
UITextField *text1 = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(label1.frame) + 20, CGRectGetMinY(label1.frame), 200, CGRectGetHeight(label1.frame))];
//text1.backgroundColor = [UIColor blueColor];
text1.placeholder = @"请输入用户名";
text1.borderStyle = 3;
text1.clearButtonMode = UITextFieldViewModeWhileEditing;
[_window addSubview:text1];
// 添加密码label
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(label1.frame),CGRectGetMaxY(label1.frame) + 30, CGRectGetWidth(label1.frame), CGRectGetHeight(label1.frame))];
label2.text = @"密码";
label2.textColor = [UIColor blackColor];
[_window addSubview:label2];
// 添加密码text
UITextField *text2 = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(text1.frame), CGRectGetMaxY(text1.frame) + 30, CGRectGetWidth(text1.frame), CGRectGetHeight(text1.frame))];
text2.placeholder = @"请输入密码";
text2.secureTextEntry = YES;
text2.borderStyle = 3;
text2.keyboardType = UIKeyboardTypeNumberPad;
text2.clearButtonMode = UITextFieldViewModeWhileEditing;
[_window addSubview:text2];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
button1.frame = CGRectMake(30, CGRectGetMaxY(label2.frame)+30, 90, 30);
[button1 setTitle:@"登录" forState:UIControlStateNormal];
[_window addSubview:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
button2.frame = CGRectMake(CGRectGetMaxX(button1.frame)+15, CGRectGetMaxY(label2.frame)+30, 90, 30);
[button2 setTitle:@"修改密码" forState:UIControlStateNormal];
[_window addSubview:button2];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
button3.frame = CGRectMake(CGRectGetMaxX(button2.frame)+15, CGRectGetMaxY(label2.frame)+30, 90, 30);
[button3 setTitle:@"注册" forState:UIControlStateNormal];
[_window addSubview:button3];