【iOS】UITextField详解和键盘设置

UITextField详解

 

一、基本属性

  1、创建文本输入框

   UITextField*textField=[[UITextField alloc]initWithFrame:CGRectMake(10, 10, 200, 20)];

 

  2、设置边框样式

  textField.borderStyle = UITextBorderStyleRoundedRect;//圆角

  可选属性:

      UITextBorderStyleNone,无边框

      UITextBorderStyleLine,有边框

      UITextBorderStyleBezel,有边框和阴影

      UITextBorderStyleRoundedRect圆角

 

  3、设置背景颜色

  textField.backgroundColor = [UIColor blueColor];

 

  4、设置背景图片

  textField.background=[UIImage imageNamed:@"test.png"];

 

  5、提示文字

  textField.placeholder = @"请输入";

 

  6、设置和读取文字内容

  textField.text = @”hello”;

  NSString * str = textField.text;

 

  7、设置字体

  [textField setFont:[UIFont fontWithName:@"Arial" size:30]];

  NSLog(@"%@", [UIFont familyNames]); 查看字体集

  

  8、密文输入

  textField.secureTextEntry = YES; 

 

  9、键盘类型

  textField.keyboardType = UIKeyboardTypeNumberPad;  数字键

  UIKeyboardTypeDefault,                   当前键盘(默认)

  UIKeyboardTypeASCIICapable,          字母输入键

  UIKeyboardTypeNumbersAndPunctuation,  数字和符号

  UIKeyboardTypeURL,                       URL键盘

  UIKeyboardTypeNumberPad,             数字键盘

  UIKeyboardTypePhonePad,               电话号码输入键盘   

  UIKeyboardTypeEmailAddress,          邮件地址输入键盘           

 

  10、键盘风格

  textView.keyboardAppearance=UIKeyboardAppearanceDefault;

  可选属性

  UIKeyboardAppearanceDefault, 默认外观,浅灰色

  UIKeyboardAppearanceAlert,     深灰 石墨色

  11、设置自定义键盘

  UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320.300)];

  //位置和宽由系统决定。

  textField.inputView = customView ;

    inputAccessoryView 设置系统键盘或自定义键盘上的视图

  12、设置左右视图

  UITextField 可以直接设置 leftView 或 rightView, 文本输入区域就在 leftView 和 rightView 之间。

  textField.leftView = leftview;

  textField.leftViewMode = UITextFieldViewModeAlways;

  右视图rightView也是一样,Mode有四种

      UITextFieldViewModeNever, 从不出现

      UITextFieldViewModeWhileEditing 编辑时显示

      UITextFieldViewModeUnlessEditing 不编辑时显示

      UITextFieldViewModeAlways  总是显示  

 

  13、设置清除按钮显示模式

      textField.clearButtonMode = UITextFieldViewModeAlways;

  Mode同左右视图的mode一样。

 

  14、再次编辑时是否清空之前内容;默认NO

  textField.clearsOnBeginEditing = YES

 

  15、对齐方式

  垂直对齐:

  textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter 

可选属性:

UIControlContentVerticalAlignmentCenter  居中对齐

UIControlContentVerticalAlignmentTop    顶部对齐,默认是顶部对齐

UIControlContentVerticalAlignmentBottom 底部对齐

UIControlContentVerticalAlignmentFill    完全填充

水平对齐:

textField.textAlignment = UITextAlignmentCenter;

可选属性:

UITextAlignmentLeft,左对齐,默认是左对齐

UITextAlignmentCenter,

UITextAlignmentRight,右对齐 

 

16、 设置滚动

textField.font = [UIFont systemFontOfSize:30]; 设置文字大小

textField.adjustsFontSizeToFitWidth = YES; 默认是NO  YES当充满边框时,文字会缩小,当小到一定程度时仍然会滚动;自适应宽度;

设置滚动时最小字号,与滚动相关,要比设置的字体小,否则没有意义,没有设置这一项文字也会缩小和滚动

textField.minimumFontSize = 20; 

 

17、设置return键

textField.returnKeyType = UIReturnKeyGoogle;search

可选属性

    UIReturnKeyDefault, 默认 灰色按钮,标有Return

    UIReturnKeyGo,      标有Go的蓝色按钮

    UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索

    UIReturnKeyJoin,标有Join的蓝色按钮

    UIReturnKeyNext,标有Next的蓝色按钮

    UIReturnKeyRoute,标有Route的蓝色按钮

    UIReturnKeySearch,标有Search的蓝色按钮

    UIReturnKeySend,标有Send的蓝色按钮

    UIReturnKeyYahoo,标有Yahoo的蓝色按钮

    UIReturnKeyYahoo,标有Yahoo的蓝色按钮

    UIReturnKeyEmergencyCall, 紧急呼叫按钮

 

18、设置输入自动纠正模式

textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;

可选属性:

    UITextAutocapitalizationTypeNone, 不自动纠正

    UITextAutocapitalizationTypeWords,  单词首字母大写

    UITextAutocapitalizationTypeSentences,  句子的首字母大写

    UITextAutocapitalizationTypeAllCharacters, 所有字母都大写

 

 

 二、文本编辑框代理

1、是否进入编辑模式

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

默认返回YES,进入编辑模式。NO不进入编辑模式

 

2、进入编辑模式

- (void)textFieldDidBeginEditing:(UITextField *)textField

 

3、是否退出编辑模式

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField

默认返回YES,退出编辑模式。NO不退出编辑模式

 

4、退出编辑模式

- (void)textFieldDidEndEditing:(UITextField *)textField

 

5、点击清除按钮是否清除

- (BOOL)textFieldShouldClear:(UITextField *)textField

默认返回YES,返回NO不清除

 

6、点击键盘上Return按钮时候调用

- (BOOL)textFieldShouldReturn:(UITextField *)textField

 

7、当输入任何字符时,代理调用该方法

-(BOOL)textField:(UITextField *)field shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

当输入字符时,代理调用该方法,如果返回YES则这次输入可以成功,如果返回NO,不能输入成功。range表示光标位置,string表示这次输入的字符串。


转自:http://www.cnblogs.com/zy1987/p/3196226.html


-----------------------------------------------分割线------------------------------------------------

一、键盘风格 
UIKit框架支持8种风格键盘。

typedef enum {

1 UIKeyboardTypeDefault,                // 默认键盘:支持所有字符  
2 UIKeyboardTypeASCIICapable,           // 支持ASCII的默认键盘  
3 UIKeyboardTypeNumbersAndPunctuation,  // 标准电话键盘,支持+*#等符号  
4 UIKeyboardTypeURL,                    // URL键盘,有.com按钮;只支持URL字符  
5 UIKeyboardTypeNumberPad,              //数字键盘  
6 UIKeyboardTypePhonePad,               // 电话键盘  
7 UIKeyboardTypeNamePhonePad,           // 电话键盘,也支持输入人名字  
8 UIKeyboardTypeEmailAddress,           // 用于输入电子邮件地址的键盘

} UIKeyboardType;

用法用例: 
textView.keyboardtype= UIKeyboardTypeNumberPad;

二、键盘外观 
typedef enum {

1 UIKeyboardAppearanceDefault,    // 默认外观:浅灰色  
2 UIKeyboardAppearanceAlert,      //深灰/石墨色

} UIKeyboardAppearance; 
用法用例: 
textView.keyboardAppearance=UIKeyboardAppearanceDefault;

三、回车键

typedef enum {

01 UIReturnKeyDefault, //默认:灰色按钮,标有Return
02 UIReturnKeyGo,  //标有Go的蓝色按钮
03 UIReturnKeyGoogle, //标有Google的蓝色按钮,用于搜索
04 UIReturnKeyJoin, //标有Join的蓝色按钮
05 UIReturnKeyNext, //标有Next的蓝色按钮
06 UIReturnKeyRoute, //标有Route的蓝色按钮
07 UIReturnKeySearch, //标有Search的蓝色按钮
08 UIReturnKeySend, //标有Send的蓝色按钮
09 UIReturnKeyYahoo, //标有Yahoo!的蓝色按钮,用于搜索
10 UIReturnKeyDone, //标有Done的蓝色按钮
11 UIReturnKeyEmergencyCall, //紧急呼叫按钮

} UIReturnKeyType; 
用法用例: 
textView.returnKeyType=UIReturnKeyGo;

四、自动大写

typedef enum {

1 UITextAutocapitalizationTypeNone, //不自动大写  
2 UITextAutocapitalizationTypeWords, //单词首字母大写  
3 UITextAutocapitalizationTypeSentences, //句子首字母大写  
4 UITextAutocapitalizationTypeAllCharacters, //所有字母大写

} UITextAutocapitalizationType; 
用法用例: 
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;

五、自动更正

typedef enum {

1 UITextAutocorrectionTypeDefault,//默认  
2 UITextAutocorrectionTypeNo,//不自动更正  
3 UITextAutocorrectionTypeYes,//自动更正

} UITextAutocorrectionType; 
用法用例: 
textField.autocorrectionType = UITextAutocorrectionTypeYes;

六、安全文本输入

textView.secureTextEntry=YES; 
开启安全输入主要是用于密码或一些私人数据的输入,此时会禁用自动更正和自此缓存。


转自:http://my.oschina.net/anyson/blog/106073


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值