@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITextField* textField1 = [[UITextField alloc] init];
//设置代理表示实现协议 UITextFieldDelegate
textField1.delegate = self;
textField1.frame = CGRectMake( 20, 120, 280, 30 );
// 边框样式
// typedef enum {
// UITextBorderStyleNone, 无边框
// UITextBorderStyleLine, 直线边框
// UITextBorderStyleBezel, 左边框、上边框加粗
// UITextBorderStyleRoundedRect 圆角
// } UITextBorderStyle;
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.text = @"hello,textfield";
textField1.placeholder = @"please input the text";
//设置字体及大小
textField1.font = [UIFont fontWithName:@"Arial" size:10.0f];
//设置字体颜色
textField1.textColor = [UIColor blueColor];
// typedef enum {
// UITextFieldViewModeNever, 重不出现
// UITextFieldViewModeWhileEditing, 编辑时出现
// UITextFieldViewModeUnlessEditing, 除了编辑外都出现
// UITextFieldViewModeAlways 一直出现
// } UITextFieldViewMode;
textField1.clearButtonMode = UITextFieldViewModeAlways;
//采用星号加密显示
textField1.secureTextEntry = NO;
//定义弹出的软键盘类型
//UIKeyboardTypeDefault 默认全部
//UIKeyboardTypeNumberPad 数字键盘
//类型较多,此处省略
textField1.keyboardType = UIKeyboardTypeDefault;
//定义软键盘上的回车按键的样式
// UIReturnKeyDefault, 默认 灰色按钮,标有Return
// UIReturnKeyGo, 标有Go的蓝色按钮
// 类型较多此处省略
textField1.returnKeyType = UIReturnKeyDefault;
//左侧或右侧增加图片
UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"person.png"]];
textField1.leftView=image;
textField1.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:textField1];
}
#pragma mark - TextField Delegates
//开始编辑时出发回调
-(void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"%s", __FUNCTION__);
}
//结束编辑时出发回调(软键盘消失)
-(void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"%s", __FUNCTION__);
}
//回车时被回调
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
NSLog(@"%s", __FUNCTION__);
[textField resignFirstResponder];//若该行注释则软件盘在回车后部消失
return YES;
}
@end