2016.01.19 UITextField

1.placeholder(占位符,就是默认提示文本)

  eg:_loginTextField.placeholder = @"QQ/手机号/邮箱";

2.borderStyle(设置边框类型)

  eg:_loginTextField.borderStyle = UITextBorderStyleRoundedRect;

    各种类型:

    typedef NS_ENUM(NSInteger, UITextBorderStyle) {

        UITextBorderStyleNone,

        UITextBorderStyleLine,

        UITextBorderStyleBezel,    

        UITextBorderStyleRoundedRect

    };

3.keyboardType(键盘类型)

  eg:_loginTextField.keyboardType = UIKeyboardTypeDefault;

    各种类型:

    typedef NS_ENUM(NSInteger, UIKeyboardType) {

        UIKeyboardTypeDefault,                

        UIKeyboardTypeASCIICapable,      

        UIKeyboardTypeNumbersAndPunctuation,

        UIKeyboardTypeURL,                    

        UIKeyboardTypeNumberPad,         

        UIKeyboardTypePhonePad,            

        UIKeyboardTypeNamePhonePad,   

        UIKeyboardTypeEmailAddress

    };

4.returnKeyType(return键样式)

  eg:_loginTextField.returnKeyType = UIReturnKeySearch;

    各种样式:

    typedef NS_ENUM(NSInteger, UIReturnKeyType) {

        UIReturnKeyDefault,

        UIReturnKeyGo,

        UIReturnKeyGoogle,

        UIReturnKeyJoin,

        UIReturnKeyNext,

        UIReturnKeyRoute,    

        UIReturnKeySearch,

        UIReturnKeySend,

        UIReturnKeyYahoo,

        UIReturnKeyDone,

        UIReturnKeyEmergencyCall,

        UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0),    //最高支持iOS9.0

    };

5.clearButtonMode(清除按钮)

  eg:_loginTextField.clearButtonMode = UITextFieldViewModeAlways;

    这样的感觉就是clearButton一直都在那,只不过是没有显示,但是clearButton和rightView只能显示一个。

6.leftView(左边的视图,rightView和leftView类似)

  eg:UIImage *searchImage = [UIImage imageNamed:@"search"];

    UIImageView *searchImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

    searchImageView.image = searchImage;

    _loginTextField.leftView = searchImageView;

    _loginTextField.leftViewMode = UITextFieldViewModeAlways;

7.UITextFiled定义了一套代理 用来监听控件的状态变化

  不过,代理的方法都是『optional』,也就是可选的,换句话就是说,不需要每种方法都写出来。

 1 //当键盘的return键被按下了
 2 - (BOOL)textFieldShouldReturn:(UITextField *)textField{
 3     //如何隐藏键盘
 4     //当点击某个textField,那么这个textField将会作为第一响应者,由于是一个textField,需要用户输入相应的内容,系统会自动弹出一个键盘。
 5     //只需要取消这个textField的第一响应者,那么系统就会隐藏键盘
 6     //[textField becomeFirstResponder];
 7     [textField resignFirstResponder];
 8     
 9     return YES;
10 }
11 
12 //当用户输入过程中,每按下一个字符,那么都会先来调用这个方法
13 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
14 //    NSLog(@"输入之前的字符串:%@", textField.text);
15 //    NSLog(@"输入了一个字符: %@", string);
16 //    NSLog(@"替换从%ld - %ld", range.location, range.length);
17     NSString *result = [textField.text stringByReplacingCharactersInRange:range withString:string];
18     NSLog(@"替换之后的结果: %@", result);
19     return YES;
20 }
21 
22 - (void)textFieldDidBeginEditing:(UITextField *)textField{
23     NSLog(@"开始编辑了");
24 }
25 
26 - (void)textFieldDidEndEditing:(UITextField *)textField{
27     NSLog(@"编辑完成");
28 }
29 
30 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
31     NSLog(@"即将开始编辑");
32     return YES;
33 }
34 
35 - (BOOL)textFieldShouldClear:(UITextField *)textField{
36     NSLog(@"清理");
37     return YES;
38 }
39 
40 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
41     NSLog(@"即将完成编辑");
42     return YES;
43 }
UITextFieldDelegate

 

8.键盘的唤起和隐藏

  在代理中的第一个方法里写到了隐藏键盘。当点击到textField时,之所以能够唤起键盘是因为,这个时候的第一响应者是textField。换而言之,只要取消了textField的第一响应者键盘也就隐藏了:『[_loginTextField resignFiestResponder];』。想要唤起键盘也只需将其变为第一响应者:『[_loginTextField becomeFirstResponder];』。

9.secureTextEntry(使用密文显示)

  eg:_hiddenTextField.secureTextEntry = YES;    //很容易发现,这个属性是一个BOOL类型的

  默认值为NO,当设为YES的时候,键盘的样式也会随之改变,没有首字母大写,没有自动修正功能。

posted @ 2016-01-20 19:27  芥末勾勒  阅读(203)  评论(0编辑  收藏  举报