zoukankan      html  css  js  c++  java
  • UITextField

    参考的文章:

    http://www.jianshu.com/p/26627f2d6cfc

    http://www.jianshu.com/p/bc2adbccc148

    记录一下UITextField限制输入的内容和输入的长度 避免长度超过限制删除失效相关问题

    分这么几步

    1.遵守协议

      UITextFieldDelegate

    2.设置代理

    3.实现代理方法

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

     

    相关代码如下:

     

    
    
    static const NSInteger kTextFieldMaxLen = 20;
     1 - (void)viewDidLoad {
     2     [super viewDidLoad];
     3     [self testTextField];
     4 }
     5 
     6 - (void)testTextField{
     7     UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 40, self.view.bounds.size.width - 40, 40)];
     8     textField.backgroundColor = [UIColor grayColor];
     9     [self.view addSubview:textField];
    10     textField.delegate = self;
    11 }
    12 
    13 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    14     //过滤非字母
    15     if([[string stringByTrimmingCharactersInSet:[NSCharacterSet letterCharacterSet]]stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]].length>0){
    16         return NO;
    17     }
    18     //当点击删除的时候在已经输入的内容超过了最大限度的情况下 能够删除
    19     if (string.length == 0) {
    20         return YES;
    21     }
    22     
    23     if (textField.text.length >= kTextFieldMaxLen) {
    24         return NO;
    25     }
    26     return YES;
    27 }

    如有错误 敬请指正

    如需转载 请注明出处 谢谢

     

     

     

     

    我会不定期分享 iOS 相关技术文章
  • 相关阅读:
    paste 合并文件
    split 分割文件
    cut 从文本中提取一段文字并输出
    tailf 跟踪日志文件
    tail 显示文件内容尾部
    给Linux系统新增加一块硬盘
    Centos7+httpd+fastcgi安装提示错误
    Redhat 7使用CentOS 7的Yum网络源
    windows7下cmd窗口使用ssh命令
    PHP set_error_handler() 函数
  • 原文地址:https://www.cnblogs.com/ITCoderW/p/7454925.html
Copyright © 2011-2022 走看看