美文网首页
UITextFile简单使用方法

UITextFile简单使用方法

作者: 小緈福 | 来源:发表于2020-03-30 16:19 被阅读0次
    {
    //初始化
    
    UITextField *text = [ [UITextField alloc] initWithFrame: CGRectMake(20, 20, 200, 200) ];  
    
    //设置边框样式  
    
    text.borderStyle = UITextBorderStyleRoundedRect;  
    
    typedefenum {  
    
          UITextBorderStyleNone;  
    
          UITextBorderStyleLine;  
    
          UITextBorderStyleBezel;  
    
          UITextBorderStyleRoundedRect;  
    
    } UITextBorderStyle;  
    
    
    
    //设置输入框的背景图片  
    
    text.background = [UIImage imageNamed: @"image.png"];  
    
    
    
    //设置输入框的背景颜色  如果使用了图片背景,会被忽略  
    
    text.backgroundColor = [UIColor blackColor];  
    
    
    
    //默认显示的提示字,水印提示  
    
    text.placeholder = @"请输入密码:";  
    
    
    
    //设置输入框一开始就有的文本  
    
    text.text = @"文本";  
    
    
    
    //设置输入框文本内容的字体样式和大小  
    
    text.font = [UIFont fontWithName:@"Arial" size: 20];  
    
    
    
    //设置字体颜色  
    
    text.textColor = [UIColor redColor];  
    
    
    
    //设置文本内容对齐方式  
    
    text.textAlignment = UITextAlignmentLeft;  
    
    //垂直对齐方式  
    
    text.contentVerticalAlignment = UIContentVerticalAlignmentLeft;  
    
    
    
    //设置为输入后变点,用于密码输入时  
    
    text.secureTextEntry = YES;  
    
    
    
    //设置文本自动纠错  
    
    text.autocorrectionType = UITextAutocorrectionTypeNo;  
    
    
    
    //设置一次性删除的叉的显示位置  
    
    text.clearButtonMode = UITextFieldViewModeAlways;  
    
    typedefenum {  
    
    UITextFieldViewModeNever;//从不出现  
    
    UITextFieldViewModeWhileEditing;//写入文本时出现  
    
    UITextFieldViewModeUnlessEditing;//除了写入文本是出现  
    
    UITextFieldViewModeAlways;//一直出现  
    
    } UITextFieldViewMode;  
    
    
    
    //再次编辑就清空  
    
    text.clearsOnBeginEditing = YES;  
    
    
    
    //文本自动适应窗口大小,  默认是保持原来大小,让长文本滚动  
    
    text.adjustsFontSizeToFitWidth = YES;  
    
    //适应窗口时最小字体大小  
    
    text.minimunFontSize =20;  
    
    
    
    //设置首字母大写  
    
    text.autocapitalizationType = UITextAutocapitailizationTypeNone;  
    
    
    
    //最左侧加图片  
    
    UIImageView *image = [ [UIImageView alloc] initWithImage: [UIImage imageNamed: @"left.png"]];  
    
    text.rightView = image;  
    
    text.rightViewMode = UITextFieldViewModeAlways;  
    
    
    
    //设置键盘样式  
    
    text.keyboardType = UIKeyboardTypeNumberPad;//数字键盘  
    
    
    
    //键盘外观  
    
    text.keyboardAppearance = UIKeyboardAppearanceDefault;  
    
    typedefenum {  
    
          UIKeyboardAppearanceDefault;  
    
    UIKeyboardAppearanceAlert;//深灰 石墨色  
    
    }UIKeyboardAppearanceType;
    
    // 限制输入长度
    
    网上有很多限制textField输入长度方法,但是我觉得都不是很完美,准确来说可以说是不符合实际开发的要求,因此在这里整理一下textField限制输入长度的方法.
    
    我所采用的并不是监听方法而是最不同的代理实现方法,为什么不使用监听呢???
    
    当你看到这篇文章很有可能视是为一件事所苦恼那就是使用监听限制输入长度后不能够完美的控制输入内容.
    
    举一个简单的例子:
    
    你要限制输入长度为30个字符,当你输入30个字符后监听的确可以很好的控制不让你继续输入,但是问题也随之而来,当你把光标移动到输入内容中间的位置后,你是可以继续输入的这种输入让人很苦恼因为当你输入后你的光标会移动到最后,并且限制你继续输入,但是你刚刚输入的内荣却保留在了文本中间,这很不符合要求.
    
    因此在这里采用代理可以很好的实现我们想要的效果,仅仅几行代码希望给你带来帮助.
    
    - (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
    
       if(textField ==self.liveThemeTextField) {
    
    //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
    
    if(range.length ==1&& string.length ==0) {
    
    returnYES;      
    
    }//so easy
    
    else if(self.liveThemeTextField.text.length >=30) {
    
    self.liveThemeTextField.text = [textField.text substringToIndex:30];
    
    returnNO;       
    
    }   
    
    }
    
    returnYES;
    
    }
    }
    

    相关文章

      网友评论

          本文标题:UITextFile简单使用方法

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