UITextField 银行卡号、千位分隔符格式化

1.在输入银行卡号的时候,每隔4位空1位。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField.tag == 100) {
        /// 银行卡格式化
        NSString *text = [textField text];
        NSString *format = @" ";
        NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
        string = [string stringByReplacingOccurrencesOfString:format withString:@""];
        if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
            return NO;
        }
    
        text = [text stringByReplacingCharactersInRange:range withString:string];
        text = [text stringByReplacingOccurrencesOfString:format 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:format];
            }
            text = [text substringFromIndex:MIN(text.length, 4)];
        }
    
        newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
    
        if (newString.length >= 24) {
            return NO;
        }
    
        [textField setText:newString];
        return NO;
    }
    else {
        return YES;
    }
}

最后将格式化的字符串再转化去掉所有空格(包括字符串首尾两头的空格)的格式
在NSString的分类中添加下面方法:

- (NSString *)cleanSpace{
    NSString * text = [self stringByReplacingOccurrencesOfString:@" " withString:@""];
    text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    return text;
}

2.在输入工资金额的时候,每隔三位数添加一个逗号(千位分隔符)

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField.tag == 101) {
    
        NSString *text = [textField text];
        NSString *format = @",";
        NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
        string = [string stringByReplacingOccurrencesOfString: format withString:@""];
        if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
            return NO;
        }
    
        text = [text stringByReplacingCharactersInRange:range withString:string];
        text = [text stringByReplacingOccurrencesOfString:@ format withString:@""];
    
        if ([text hasPrefix:@"0"]) {
            return NO;
        }
    
        NSMutableString *newString = [NSMutableString stringWithString:@""];
    
        while (text.length > 0) {
            NSRange subRange = NSMakeRange(text.length - MIN(text.length, 3), MIN(text.length, 3));
            NSString * subString = [text substringWithRange:subRange];
            [newString insertString:subString atIndex:0];
            if (subString.length == 3) {
                [newString insertString:@ format atIndex:0];
            }
            text = [text substringToIndex:text.length - subString.length];
        }
    
        NSString * resultString = [newString copy];
        resultString = [resultString stringByTrimmingCharactersInSet:[characterSet invertedSet]];

        if (resultString.length >= 16) {
            return NO;
        }
    
        [textField setText:resultString];
        return NO;
    }
}

最后将格式化的字符串再转化去掉千位分隔符的格式
在NSString的分类中添加下面方法:

- (NSString *)salary{
    NSString * text = [self stringByReplacingOccurrencesOfString:@"," withString:@""];
    text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    return text;
}

你可能感兴趣的:(UITextField 银行卡号、千位分隔符格式化)