zoukankan      html  css  js  c++  java
  • UITextField基础语法

     

    #pragma mark -  1.创建TextField

     

    - (void)createTextField {

        

        UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(20, 40, 250, 45)];

        textFiled.tag          = 1;

        [self.view addSubview:textFiled];

    }

     

    #pragma mark - 2.设置TextFieldOutLook

     

    - (void)setUpTextFieldOutLook {

       

        UITextField *textField = [self.view viewWithTag:1];

        

        //设置边框

        /*

         UITextBorderStyleNone,

         UITextBorderStyleLine,  线性边框

         UITextBorderStyleBezel, 斜射边框

         UITextBorderStyleRoundedRect  圆角边框,显示不出背景图片

         */

        textField.borderStyle = UITextBorderStyleNone;//在圆角边框下,显示不出背景图片

        

        //设置背景图片

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

        

        //禁止用户操作  enabled使能够

        textField.enabled = NO;//设置为NO点击就不响应了

        //设置不可操作下的图片。点击无用

        textField.disabledBackground = [UIImage imageNamed:@"dove2"];

        

        textField.enabled = YES;// 改为Yes 后,可以响应,但图片是enabled情况下的图片dove.png

    }

     

    #pragma mark - 3.设置文本

     

    - (void)setText {

        

        UITextField *textField  = [self.view viewWithTag:1];

        

        // 1.基本文本样式设置

        textField.placeholder   = @"请输入账号";

        textField.font          = [UIFont boldSystemFontOfSize:16];

        textField.textColor     = [UIColor redColor];

        textField.textAlignment = NSTextAlignmentLeft;

     

        // 2.显示清空按钮

        /*

         UITextFieldViewModeNever,

         UITextFieldViewModeWhileEditing,

         UITextFieldViewModeUnlessEditing,

         UITextFieldViewModeAlways

         */

        textField.clearButtonMode = UITextFieldViewModeAlways;

        

        //3.设置文字自适应内容的宽度,与最小字体

        textField.adjustsFontSizeToFitWidth = YES;

        textField.minimumFontSize           = 5;

        

        //4.设置左边,右边的图片

        UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];

        leftImageView.image    = [UIImage imageNamed:@"qq"];

        textField.leftView     = leftImageView;

        textField.leftViewMode = UITextFieldViewModeAlways;

        

        UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];

        rightImageView.image    = [UIImage imageNamed:@"weixin"];

        textField.rightView     = rightImageView;

        textField.rightViewMode = UITextFieldViewModeAlways;

        

        //5.设置密文显示

        //textField.secureTextEntry = YES;

        

        //代码设置自动获取焦点,如果不设置就没有光标显示,点击之后才有显示

        [textField becomeFirstResponder];

        

        //6.设置单词字母是否大写

        /*

         UITextAutocapitalizationTypeNone,没有

         UITextAutocapitalizationTypeWords, 每一个单词首字母大写

         UITextAutocapitalizationTypeSentences, 每一个句子的首字母

         UITextAutocapitalizationTypeAllCharacters,每个英文字母都大写

         */

        textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;

        

        //7.是否纠错

        /*

         UITextAutocorrectionTypeDefault,

         UITextAutocorrectionTypeNo,

         UITextAutocorrectionTypeYes,

         */

        textField.autocorrectionType = UITextAutocorrectionTypeYes;

    }

     

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

     

        UITextField * textField = (UITextField*)[self.view viewWithTag:1];

        //失去焦点

        [textField resignFirstResponder];

    }

     

    #pragma mark - 4.设置键盘

     

    - (void)setkeyboard {

     

        UITextField *textField  = [self.view viewWithTag:1];

        

        // 修改键盘的颜色

        /*

         UIKeyboardAppearanceDefault,          // Default apperance for the current input method.

         UIKeyboardAppearanceDark NS_ENUM_AVAILABLE_IOS(7_0),

         UIKeyboardAppearanceLight NS_ENUM_AVAILABLE_IOS(7_0),

         UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark,  // Deprecated

         */

        textField.keyboardAppearance = UIKeyboardAppearanceDefault;

        

        //键盘的样式

        textField.keyboardType = UIKeyboardTypeDefault;

     

        // 设置inputviewinputAccessoryView

        UIView *view         = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];

        view.backgroundColor = [UIColor brownColor];

        textField.inputView  = view;

     

        UIView *view1         = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];

        view1.backgroundColor = [UIColor blueColor];

        textField.inputAccessoryView  = view1;

    }

  • 相关阅读:
    TP5报如下的错误 Indirect modification of overloaded element of thinkpaginatorCollection has no effect【转】
    PHPnumber_format()函数的使用
    蓝桥杯-PREV28-地宫取宝
    HihoCode-1053-居民迁移
    HihoCode-1323-回文字符串
    蓝桥杯-PREV3-带分数
    HDU-6312-Game
    HDU-6438-Buy and Resell
    CF-1117C-Magic Ship
    HDU-2802-F(N)
  • 原文地址:https://www.cnblogs.com/makingitbest/p/6393841.html
Copyright © 2011-2022 走看看