• 使用UITextField自动格式化银行卡号


    -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {  
      
       NSString *text = [textField text];  
      
        NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];  
        string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];  
        if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {  
            return NO;  
        }  
      
        text = [text stringByReplacingCharactersInRange:range withString:string];  
        text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];  
      
        NSString *newString = @"";  
        while (text.length > 0) {  
            NSString *subString = [text substringToIndex:MIN(text.length, 4)];  
            newString = [newString stringByAppendingString:subString];  
            if (subString.length == 4) {  
                newString = [newString stringByAppendingString:@" "];  
            }  
            text = [text substringFromIndex:MIN(text.length, 4)];  
        }  
      
        newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];  
      
        if (newString.length >= 20) {  
            return NO;  
        }  
      
        [textField setText:newString];  
      
        return NO;  
    }
    // 16位以内 (3个空格)  
            NSString *str_bangNum = [NSString stringWithFormat:@"%@%@",textField.text,string];  
            if (textField.text.length < 23) {  
                //银行卡只能输入数字  
                cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];  
                NSString *filterStr = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];  
                BOOL basicTest = [string isEqualToString:filterStr];  
                if(!basicTest)  
                {  
                    return NO;  
                }  
                  
                //显示内容的格式化  
                if(range.length > 0 && str_bangNum.length > 0)// 支持删除  
                {  
                    NSString *tmp_str = [str_bangNum bankNumToNormalNum];  
                    str_bangNum = [tmp_str substringToIndex:(tmp_str.length - 1)];  
                }  
                str_bangNum = [str_bangNum normalNumToBankNum];  
                textField.text = str_bangNum;  
                return NO;  
            }  
            else  
            {  
                if(range.length > 0 && str_bangNum.length > 0)// 支持删除  
                {  
                    NSString *tmp_str = [str_bangNum bankNumToNormalNum];  
                    str_bangNum = [tmp_str substringToIndex:(tmp_str.length - 1)];  
                    str_bangNum = [str_bangNum normalNumToBankNum];  
                    textField.text = str_bangNum;  
                }  
                return NO;  
            }
    // 正常号转银行卡号 - 增加4位间的空格  
    -(NSString *)normalNumToBankNum  
    {  
         NSString *tmpStr = [self bankNumToNormalNum];  
          
        int size = (tmpStr.length / 4);  
         
        NSMutableArray *tmpStrArr = [[NSMutableArray alloc] init];  
        for (int n = 0;n < size; n++)  
        {  
            [tmpStrArr addObject:[tmpStr substringWithRange:NSMakeRange(n*4, 4)]];  
        }  
          
        [tmpStrArr addObject:[tmpStr substringWithRange:NSMakeRange(size*4, (tmpStr.length % 4))]];  
          
        tmpStr = [tmpStrArr componentsJoinedByString:@" "];  
      
        return tmpStr;  
    }  
      
    // 银行卡号转正常号 - 去除4位间的空格  
    -(NSString *)bankNumToNormalNum  
    {  
        return [self stringByReplacingOccurrencesOfString:@" " withString:@""];  
    }  

    http://blog.csdn.net/h3c4lenovo/article/details/8515304

  • 相关阅读:
    数组-11. 猴子选大王
    *数组-10. 求整数序列中出现次数最多的数
    数组-07. 求一批整数中出现最多的个位数字
    *数组-05. 字符串字母大小写转换
    数组-04. 查找整数
    《结对-网页贪吃蛇-最终程序》
    Forward团队-爬虫豆瓣top250项目-项目进度
    《结对-HTML贪吃蛇游戏项目-测试过程》
    课后作业-阅读任务-阅读提问-2
    《20171005-构建之法:现代软件工程-阅读笔记》
  • 原文地址:https://www.cnblogs.com/why-not/p/4782367.html
Copyright © 2020-2023  润新知