• iOS UITextField限制输入长度


    这篇博客主要讲限制输入长度的问题,前几天有人问我这个问题,说限制长度会出现无法删除问题,于是正好一块发出来给大家看看。textField的缩进,一张背景图片搞定的事,我这里用了leftView纯属附带。

    好了废话少说,贴代码,很简单,大家一看便知。

    //先创建一个textField 和 一个button。

    #import "ViewController.h"
    
    @interface ViewController ()<UITextFieldDelegate> {
        
        UITextField *currentTextFeild;
        UIButton    *touchButton;
    }
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        UITextField *textFields = [[UITextField alloc] initWithFrame:CGRectMake(15, 50, self.view.bounds.size.width-15*2, 40)];
        textFields.backgroundColor = [UIColor brownColor];
        textFields.layer.cornerRadius = 5;
        textFields.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 40)];
        textFields.leftViewMode = UITextFieldViewModeAlways;//这两行是为了不让Text太贴textField的左边
        textFields.placeholder = @"请输入手机号";
        textFields.delegate = self;
        [self.view addSubview:textFields];
        currentTextFeild = textFields;
        
        UIButton *enableButton = [UIButton buttonWithType:UIButtonTypeCustom];
        enableButton.frame = CGRectMake(15, 100, self.view.bounds.size.width-15*2, 40);
        enableButton.layer.cornerRadius = 5;
        enableButton.backgroundColor = [UIColor grayColor];
        [enableButton setTitle:@"没内容不可点击" forState:UIControlStateNormal];
        [enableButton setTitle:@"可以按了" forState:UIControlStateSelected];
        [enableButton setTitle:@"按下去了" forState:UIControlStateHighlighted];
        enableButton.enabled = NO;
        [enableButton addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:enableButton];
        touchButton = enableButton;
    }
    
    - (void)btnClick {
        
        
    }
    

    //设置textField代理 

    #pragma mark -  UITextFieldDelegate
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
        
        return YES;
    }
    
    - (void)textFieldDidEndEditing:(UITextField *)textField {
        
        
    }
    
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
        
        //用来判断是否可以继续输入, - range.length是为了判断是否可以删除
        NSInteger currentLength = textField.text.length - range.length + string.length;
        if (currentLength > 11) {
            
            return NO;
        }
        
        //判断按钮是否可以enable = YES
        if (currentTextFeild.text && currentTextFeild.text.length > 0 && currentLength > 0) {
            
            touchButton.enabled = YES;
            touchButton.selected = YES;
        }else {
            
            touchButton.enabled = NO;
            touchButton.selected = NO;
        }
        
        if (currentLength <= 0) {
            
            touchButton.enabled = NO;
            touchButton.selected = NO;
        }
        
        return YES;
    }
    
    - (BOOL)textFieldShouldClear:(UITextField *)textField {
        
        if (currentTextFeild.tag == 11 || currentTextFeild.tag == 12) {
            //手机号
            touchButton.enabled = NO;
            touchButton.selected = NO;;
        }
        
        return YES;
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        
        [textField resignFirstResponder];
        
        return YES;
    }
    

     大家看了代码,差不多就明白了。

  • 相关阅读:
    JAVA实现AES的加密和解密算法
    工厂方法模式(Factory Method)
    Java开发23种设计模式
    读取Maven项目下resources目录下的配置文件(properties为例)
    迭代解析JSON简单实例
    Tomcat8.0配置JNDI多数据源
    SpringMVC DispatcherServlet-------视图渲染过程
    【C/C++】求解线性方程组的雅克比迭代与高斯赛德尔迭代
    【C/C++】实现牛顿迭代
    【C/C++】查找(一):静态查找表
  • 原文地址:https://www.cnblogs.com/wly314/p/4682097.html
Copyright © 2020-2023  润新知