• 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的时候,键盘的样式也会随之改变,没有首字母大写,没有自动修正功能。

  • 相关阅读:
    GAE 1.5.1 SDK Prerelease
    GAE 1.5.1 SDK Prerelease
    SVN 分支更新与合并
    暂时告别 CSDN 博客,移居 GAE(http://88250.b3log.org)
    暂时告别 CSDN 博客,移居 GAE(http://88250.b3log.org)
    NetBeans 时事通讯(刊号 # 152 Jun 22, 2011)
    NetBeans 时事通讯(刊号 # 152 Jun 22, 2011)
    NetBeans 时事通讯(刊号 # 151 Jun 15, 2011)
    SVN 分支更新与合并
    NetBeans 时事通讯(刊号 # 151 Jun 15, 2011)
  • 原文地址:https://www.cnblogs.com/immustard/p/5146325.html
Copyright © 2020-2023  润新知