美文网首页
UITextField详解

UITextField详解

作者: songsongchen | 来源:发表于2017-01-05 11:03 被阅读129次

    2017.1.5

    1. UITextField属性

    _textField =  [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 150, 20)];

    文本内容     _textField.text = @"UITextField 详解"; 

    设置Text样式 

    NSMutableAttributedString  *attributedString=[[NSMutableAttributedString  alloc]  initWithString:_textField.text];

    NSRange range = NSMakeRange(0, _textField.text.length);

    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20]  range:range];

    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor]  range:range];

    _textField.attributedText = attributedString;

    设置文本颜色  _textField.textColor = [UIColor blueColor];

    设置字体大小  _textField.font = [UIFont systemFontOfSize:30];

    对齐方式    _textField.textAlignment = NSTextAlignmentLeft;枚举

    边框样式 _textField.borderStyle = UITextBorderStyleRoundedRect;枚举

    设置默认的Text的样式

    _textField.defaultTextAttributes = @{NSForegroundColorAttributeName:[UIColor blackColor],NSUnderlineStyleAttributeName:@2};

    text属性为空时默认显示   _textField.placeholder = @"placeholder";

    设置placeholder文本属性 _textField.attributedPlaceholder = _textField.attributedText;

    成为焦点时文本内容清空  _textField.clearsOnBeginEditing = YES;

    根据宽度自动适应字体大小  _textField.adjustsFontSizeToFitWidth = YES;

    最小的字体大小   _textField.minimumFontSize = 14;

    设置背景图  textField.background = [UIImage imageNamed:@"image.jpg"];

    设置不可用时的背景图 若background未设置则忽略

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

    是否可以更改字符属性字典_textField.allowsEditingTextAttributes = YES;

    属性字典  _textField.typingAttributes = _textField.defaultTextAttributes;

    设置清除按钮的显示样式  _textField.clearButtonMode = UITextFieldViewModeAlways;枚举

    左视图 右视图

    UIView *leftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 40)];

    leftView.backgroundColor=[UIColor redColor];

    _textField.leftView=leftView;

    设置左视图也不显示 需要设置leftViewMode  _textField.leftViewMode=UITextFieldViewModeAlways;枚举

    设置弹出视图 _textField.inputView = self.inputView;

    设置弹出辅助视图  _textField.inputAccessoryView = self.inputAccessoryView;

    设置是否允许再次编辑时在内容中间插入内容 _textField.clearsOnInsertion = YES;

    UITextFiled和 UITextView都遵循 UITextInputTraits协议,在UITextInputTraits协议中定义了设置键盘的属性

    键盘类型  枚举  _textField.keyboardType=UIKeyboardTypeNumbersAndPunctuation;枚举

    键盘Return键显示的文本,默认为”Return”,其他可选择的包括Send,Next,,Go,Done,Google等

    _textField.returnKeyType = UIReturnKeyDone;

    键盘外观   _textField.keyboardAppearance = UIKeyboardAppearanceLight;

    文本大小写样式 _textField.autocapitalizationType = UITextAutocapitalizationTypeWords;

    是否自动更正_textField.autocorrectionType = UITextAutocorrectionTypeYes;

    拼写检查设置  _textField.spellCheckingType = UITextSpellCheckingTypeYes;

    是否在无文本时禁用Return键,默认为NO。若为YES,则用户至少输入一个字符后Return键才被激活

    _textField.enablesReturnKeyAutomatically = YES;

    若输入的是密码,可设置此类型为YES,输入字符时可显示最后一个字符,其他字符显示为点

    _textField.secureTextEntry=YES;

    2.UITextFieldDelegate

    设置代理  _textField.delegate  =  self;

    点击输入框时触发的方法,返回YES则可以进入编辑状态,NO则不能

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

            returnYES;

    }

    开始编辑时调用的方法

    - (void)textFieldDidBeginEditing:(UITextField *)textField  {

    }

    将要结束编辑时调用的方法,返回YES则可以结束编辑状态,NO则不能

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

     returnYES;

    }

    结束编辑调用的方法

    - (void)textFieldDidEndEditing:(UITextField *)textField  {

    NSLog(@"结束编辑 textFieldDidEndEditing");

    }

    输入字符时调用的方法

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

    returnYES;

    }

    点击清除按钮时调用的函数,返回YES则可以清除,点击NO则不能清除

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

        returnYES;

    }

    点击return键触发的函数

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

        [_textField resignFirstResponder];//失去第一响应

    returnYES;

    }

    更多请查看官方Api

    相关文章

      网友评论

          本文标题:UITextField详解

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