美文网首页
iOS开发之五:常用控件--UITextField的使用

iOS开发之五:常用控件--UITextField的使用

作者: 咖啡绿茶1991 | 来源:发表于2018-05-03 17:30 被阅读0次

    UITextField 是iOS开发中用的非常多的一种控件,主要是供用户输入单行信息的。下面来详细介绍UITextField。

    1、常用属性

    // 设置和获取文本内容,默认nil  

    @property(nonatomic,copy) NSString *text;  

    // 设置文本内容颜色  

    @property(nonatomic,retain) UIColor *textColor;  

    // 设置字体  

    @property(nonatomic,retain) UIFont *font  

    // 对齐样式  

    @property(nonatomic) NSTextAlignment textAlignment;  

    // 设置风格,默认没有风格,需要设置  

    @property(nonatomic) UITextBorderStyle borderStyle;  

    // 提示用户输入内容文本  

    @property(nonatomic,copy) NSString *placeholder;  

    // 用户编辑时是否clear内容,默认为NO  

    @property(nonatomic) BOOL clearsOnBeginEditing;  

    // 自适应调整字体大小,默认为NO  

    @property(nonatomic) BOOL adjustsFontSizeToFitWidth;  

    // 设置代理  

    @property(nonatomic,assign) id delegate;  

    // 设置背景,需要将textField实例的风格设置为None  

    @property(nonatomic,retain) UIImage *background;  

    // 设置textField不可用时的背景图片  

    @property(nonatomic,retain) UIImage *disabledBackground;  

    // 设置是否可编辑  

    @property(nonatomic,readonly,getter=isEditing) BOOL editing;  

    // 清除按钮的模式,默认不出现  

    @property(nonatomic) UITextFieldViewMode clearButtonMode;  

    // 自定义左视图  

    @property(nonatomic,retain) UIView *leftView;  

    // 自定义左视图出现的模式  

    @property(nonatomic) UITextFieldViewMode leftViewMode;  

    // 不用系统键盘,自定义键盘  

    @property (readwrite, retain) UIView *inputView;  

    // 系统键盘和自定义键盘共存  

    @property (readwrite, retain) UIView *inputAccessoryView;  

    // 自动大写类型  

    @property(nonatomic) UITextAutocapitalizationType autocapitalizationType;  

    // 检查拼写是否正确  

    @property(nonatomic) UITextAutocorrectionType autocorrectionType;  

    // 修改键盘类型  

    @property(nonatomic) UIKeyboardType keyboardType;  

    // 修改返回类型  

    @property(nonatomic) UIReturnKeyType returnKeyType;  

    // 是否安全输入,比如用户输入密码  

    @property(nonatomic,getter=isSecureTextEntry) BOOL secureTextEntry; 

    iOS中选中输入框会自动弹出键盘,但是如果需要关闭键盘则需要自己手动控制,一般关闭键盘,要么在点击returnKeyType的时候用代理方法关闭,或者点击真个View的空白区域来关闭。

    关闭键盘的方法,点参照我的另一片文章-----IOS隐藏键盘的几种方式

    2、常用的代理方法

    // 将要开始输入时调用  

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {  

    NSLog(@"开始输入");  

    return YES;  

    }  

    // 将要输入结束时调用  

    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {  

    NSLog(@"输入结束");  

    return YES;  

    }  

    // 清除文字按钮点击事件  

    - (BOOL)textFieldShouldClear:(UITextField *)textField {  

    NSLog(@"清除输入内容了");  

    return YES;  

    }  

    // 键盘上的return按钮  

    - (BOOL)textFieldShouldReturn:(UITextField *)textField {  

    //隐藏输入键盘  

    [textField resignFirstResponder];  

    return YES;  

    }

    创建UITextField,如果用代码创建的话,实例代码如下:

    UITextField *textfield = [[UITextField alloc]  

    initWithFrame:CGRectMake(20, 240, 200, 30)];  

    // 禁止首字母大写  

    textfield.autocapitalizationType = UITextAutocapitalizationTypeNone;  

    // 设置键盘类型  

    textfield.keyboardType = UIKeyboardTypeNamePhonePad;  

    // 输入框的边框类型  

    textfield.borderStyle = UITextBorderStyleRoundedRect;  

    // 设置委托代理  

    textfield.delegate = self;  

    // 键盘上的return按钮类型  

    textfield.returnKeyType = UIReturnKeyDone;  

    // 是否安全输入,是的话,输入内容将为星号  

    textfield.secureTextEntry = NO;  

    // 清除按钮模式  

    textfield.clearButtonMode = UITextFieldViewModeAlways;  

    // 输入框中的文本颜色  

    textfield.textColor = [UIColor redColor];  

    // 输入框的字体  

    textfield.font = [UIFont boldSystemFontOfSize:14];

    UITextField 好像用法比较简单,唯一需要注意的就是键盘的隐藏的,其他的特殊功能,可能还没用到吧,以后遇到相关问题再来补充记录。

    版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/u011619283/article/details/23939367

    相关文章

      网友评论

          本文标题:iOS开发之五:常用控件--UITextField的使用

          本文链接:https://www.haomeiwen.com/subject/aywkrftx.html