UITextField* textField=[[UITextField alloc]initWithFrame:CGRectMake(60, 100, 200, 30)]; //属性 //样式 textField.borderStyle=UITextBorderStyleBezel;//矩形有阴影 textField.borderStyle=UITextBorderStyleLine;//矩形没有阴影 textField.borderStyle=UITextBorderStyleNone;//无边框 textField.borderStyle=UITextBorderStyleRoundedRect;//边角为圆形 textField.text=@"sdf";//默认字体 textField.placeholder=@"在此输入。。。";//提示输入 textField.textAlignment=NSTextAlignmentCenter;//居中 textField.secureTextEntry=YES;//安全输入,密码框 textField.clearButtonMode=UITextFieldViewModeWhileEditing;//设置文本框右边有个清除栏 textField.keyboardType = UIKeyboardTypePhonePad; // 弹出的键盘的样式,弹出数组键盘 textField.keyboardAppearance=UIKeyboardAppearanceAlert;//设置键盘的颜色为深灰,默认是浅灰色UIKeyboardAppearanceDefault textField.clearsOnBeginEditing = YES; // 再次编辑就清空原有的内容 textField.minimumFontSize = 20.0f; // 设置自动缩小显示的最小字体大小 textField.secureTextEntry = NO; // 设置是否以密码的圆点形式显示 textField.autocorrectionType = YES; // 设置是否启动自动提醒更新功能 textField.returnKeyType = UIReturnKeyDefault; // 设置弹出的键盘带形式与带的按键
[textField resignFirstResponder];//结束编辑
[self.view addSubview:textField];
//当开始点击textField会调用的方法 -(void)textFieldDidBeginEditing:(UITextField *)textField { NSLog(@"开始编辑"); } //当textField编辑结束时调用的方法 -(void)textFieldDidEndEditing:(UITextField *)textField { NSLog(@"结束编辑"); } //按下Done按钮的调用方法,我们让键盘消失 -(BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
记得在ViewController.h中添加代理UITextFieldDelegate
textField.delegate=self;//设置代理为自身