zoukankan      html  css  js  c++  java
  • UITextField限制字数的方法

    在输入东西的时候,如果想限制最大字数,可以用下面方法

      - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; 
        { 
            if ([string isEqualToString:@"\n"])  
            { 
                return YES; 
            } 
            NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; 
             
            if (self.myTextField == textField)  
            { 
                if ([toBeString length] > 20) { 
                    textField.text = [toBeString substringToIndex:20]; 
                    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:nil message:@"超过最大字数不能输入了" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] autorelease]; 
                    [alert show]; 
                    return NO; 
                } 
            } 
            return YES; 
        }  

  • 相关阅读:
    2016第5周四
    2016第5周三
    2016第5周二
    HTTP2.0那些事
    2016第4周日
    【C语言的日常实践(十二)】命令行参数
    Oracle改变字段类型
    Codeforces Round #269 (Div. 2)
    linux shell 命令
    Codeforces Round #256 (Div. 2) C. Painting Fence 或搜索DP
  • 原文地址:https://www.cnblogs.com/careerman/p/2645315.html
Copyright © 2011-2022 走看看