UITextField: 在UILabel的显示文字基础上, 多了文字的编辑功能, 可以允许用户输入以及编辑文字. 继承自UIControl
UITextField使用步骤:
1, 创建控件
2, 配置属性
3, 添加到父视图
4, 释放(取决于第一步有没有alloc)
//1.创建对象
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 300, 38)];
//2.配置属性
textField.backgroundColor = [UIColor greenColor];
//(1)设置输入框的占位符(提示文字)
textField.placeholder = @"请输入三围";
//(2)设置输入框显示的文字
textField.text = @"38尺";
//(3)设置字体颜色
textField.textColor = [UIColor whiteColor];
//(4)设置文字的样式
textField.font = [UIFont systemFontOfSize:30];
textField.font = [UIFont fontWithName:@"ChalkboardSE-Regular" size:17];
//(5)设置文字的对齐方式
textField.textAlignment = NSTextAlignmentCenter;
//(6)是否允许输入
textField.enabled = YES;//默认为YES -- 允许编辑.
//(7)是否开始输入的时候清空输入框内容
textField.clearsOnBeginEditing = YES;//默认为 NO -- 不会清除输入框中的内容
//(8)是否文字以圆点格式显示
textField.secureTextEntry = YES;//默认为NO -- 非密文输入格式
//(9)弹出键盘的类型(枚举值)
// textField.keyboardType = UIKeyboardTypeNumberPad;
//(10)键盘右下角return按钮类型(枚举值)
textField.returnKeyType = UIReturnKeyGo;
//(11)自定义输入视图(默认是键盘)
/*
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 3, 300)];
redView.backgroundColor = [UIColor redColor];
textField.inputView = redView;//当需要自定义键盘时可以设置该属性
[redView release];
*/
//(12)输入视图上方的辅助视图(默认nil)
UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
yellowView.backgroundColor = [UIColor yellowColor];
textField.inputAccessoryView = yellowView;
[yellowView release];
//(13)边框样式(枚举值)
textField.borderStyle = UITextBorderStyleRoundedRect;
//(14)清除按钮模式(枚举值)
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
//(15)输入框左视图
UIView *leftVW = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 100)];
leftVW.backgroundColor = [UIColor redColor];
textField.leftView = leftVW;
[leftVW release];
//(16)左视图的显示模式
textField.leftViewMode = UITextFieldViewModeAlways;
//(17)输入框右视图
UIView *rightVW = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 38)];
rightVW.backgroundColor = [UIColor cyanColor];
textField.rightView = rightVW;
[rightVW release];
//(18)右视图的显示模式
textField.rightViewMode = UITextFieldViewModeAlways;
//(19)设置输入框代理
textField.delegate = self;
//(20)设置tag值
textField.tag = 200;
//3.添加到父视图
[contentView addSubview:textField];
//4.释放
[textField release];