UITextField

//输入框-开发应用场景:注册、登录,能够展示文本数据,并且能够与用户进行交互

//对齐方式(参考label)
@property(nonatomic) NSTextAlignment textAlignment;
//设置文本框的边框风格,设置为圆角矩形边框风格,默认风格为UITextBorderStyleNone
@property(nonatomic) UITextBorderStyle borderStyle;
//背景文字
@property(nonatomic,copy) NSString *placeholder;
//每次开始编辑的时候都清空之前的内容
@property(nonatomic) BOOL clearsOnBeginEditing; 
//超过以后自动缩小
@property(nonatomic) BOOL adjustsFontSizeToFitWidth;
//最小只能缩小的倍数
@property(nonatomic) CGFloat minimumFontSize;
//垂直对齐方式
@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment;
//背景图
@property(nonatomic,retain) UIImage *background; 
//清空按钮的出现模式
@property(nonatomic) UITextFieldViewMode  clearButtonMode;
//键盘模式
@property(nonatomic) UIKeyboardType keyboardType;
//回车键模式
@property(nonatomic) UIReturnKeyType returnKeyType;
//安全输入模式(暗文加密)
@property(nonatomic,getter=isSecureTextEntry) BOOL secureTextEntry;
//正文
@property(nonatomic,copy) NSString *text;

- (BOOL)becomeFirstResponder;//响应输入状态
- (BOOL)resignFirstResponder;//结束输入状态


//高级功能,暂时了解一下就行。
//左view及出现模式
@property(nonatomic,retain) UIView *leftView; 
@property(nonatomic) UITextFieldViewMode leftViewMode; 
//右view及出现模式
@property(nonatomic,retain) UIView *rightView;   
@property(nonatomic) UITextFieldViewMode rightViewMode;
//这个属性默认就是键盘,如果设置某个view那么弹出来的就不是键盘,而是自己设置的view
@property (readwrite, retain) UIView *inputView;
//这个view是随着键盘一想弹出的view
@property (readwrite, retain) UIView *inputAccessoryView;

UITextFieldDelegate 【代理方法】
//开始编辑
- (void)textFieldDidBeginEditing:(UITextField *)textField;
//结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField;
//回车时触发
- (BOOL)textFieldShouldReturn:(UITextField *)textField;



    UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(10,30,300,50)];
    //设置显示的样式(圆角矩形)
    field.borderStyle = UITextBorderStyleRoundedRect;
    field.backgroundColor = [UIColor orangeColor];
    //placeholder 设置textField默认的提示文字
    field.placeholder = @"请输入文字";
    //当编辑文字的时候,出现清除按钮
    field.clearButtonMode = UITextFieldViewModeWhileEditing;
    //设置明文、暗文显示,默认为明文
    field.secureTextEntry = NO;//设置密码用到
    //设置弹出键盘的样式
    //UIKeyboardTypeNumberPad 纯数字键盘
    field.keyboardType = UIKeyboardTypeNumberPad;
    field.keyboardType = UIKeyboardTypeDefault;
    //更改右下角按键的样式
    field.returnKeyType  = UIReturnKeyDone;
    //设置field的代理
    field.delegate = self;
    //成为第一响应者,UITextField会优先处理用户交互,键盘会自动弹出
    [field becomeFirstResponder];
    [self.view addSubview:field];
    //UITextField为相对复杂的UI控件,UIKit中已经定义好了UITextFieldDelegate
    // Do any additional setup after loading the view.
}
//当用户开始对textField进行编辑时(键盘弹出时),触发此方法
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"did begin editing!");
}
//当结束编辑时(键盘收回的时候),触发此方法
- (void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"did end editing!");
}
//当点击右下角的按钮时,触发此方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    NSLog(@"return!");
    NSLog(@"输入的内容:%@",textField.text);
    //收键盘 (取消第一响应者)
    [textField resignFirstResponder];
    return YES;
}

//点击其他地方收起键盘
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
原文地址:https://www.cnblogs.com/liudongyan/p/4399295.html