1.创建对象
UITextField *fTextfield = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];2.配置属性
//(1)背景颜色fTextfield.backgroundColor = [UIColor yellowColor];
//(2)边框样式
fTextfield.borderStyle = UITextBorderStyleRoundedRect;
//(3)占位文字----为输入框提供提示
fTextfield.placeholder = @"请输入用户名";
//userNameTextFiled.text = @"wenzi ";
//(4)设置文本
fTextfield.text = @"这是文本框";
//(5)文本颜色
fTextfield.textColor = [UIColor blueColor];
//(6)文本对齐方式
fTextfield.textAlignment = NSTextAlignmentCenter;
//(7)字体设置
fTextfield.font = [UIFont systemFontOfSize:18];
//*********输入控制
//(8)是否允许输⼊
fTextfield.enabled = YES;
//(9)是否开始输⼊的时候清空输入框内容
fTextfield.clearsOnBeginEditing = YES;
//(10)是否⽂字以圆点格式显⽰----密文格式
fTextfield.secureTextEntry = YES;
//(11)弹出键盘的类型
fTextfield.keyboardType = UIKeyboardTypeNamePhonePad;
//(12)returnkeytype
fTextfield.returnKeyType = UIReturnKeyGo;
/*
//(13)inputView(可以自定义键盘,替换原来的键盘)nice
//自定义有一个键盘view *类型的
UIView *myPutView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
myPutView.backgroundColor = [UIColor redColor];
//替换原来的键盘
userNameTextFiled.inputView = myPutView;
//(14)inputAccessoryView
//自定义一个view
UIView *myAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
myAccessoryView.backgroundColor = [UIColor purpleColor];
//替换原来的
userNameTextFiled.inputAccessoryView = myAccessoryView;
*/
//********外观设置
//(15)清除按钮模式(√)
fTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;//(16)设置左视图的模式
//自定义一个view,用于显示在leftView的位置
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 10, 10)];
leftView.backgroundColor = [UIColor cyanColor];
fTextfield.leftView = leftView;
//(17)设置显示模式
fTextfield.leftViewMode = UITextFieldViewModeAlways;
//设置右视图
//自定义一个view
UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
rightView.backgroundColor = [UIColor blueColor];
fTextfield.rightView = rightView;
fTextfield.rightViewMode = UITextFieldViewModeAlways;
3.代理方法
fTextfield.delegate = self;
实现点击return键触发的代理方法回收键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//释放第一响应者
[textField resignFirstResponder];//也可以用结束编辑这个方法
[textField endEditing:YES];
return YES;
}