NSNumber *number = [NSNumber numberWithLongLong:1234567890098765];
NSNumberFormatter *formatter = [NSNumberFormatter new];
[formatter setUsesGroupingSeparator:YES];设置用组分隔
[formatter setGroupingSize:4];//四个一组
[formatter setGroupingSeparator:@" "];组间用空格隔开
NSString *string = [formatter stringFromNumber:number];
string : 1234 5678 9009 8765
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField == bankCardNumTextField) {
// 四位加一个空格
if ([string isEqualToString:@""]) { // 删除字符
if ((textField.text.length - 2) % 5 == 0) {
textField.text = [textField.text substringToIndex:textField.text.length - 1];
}
return YES;
} else {
if (textField.text.length % 5 == 0) {
textField.text = [NSString stringWithFormat:@"%@ ", textField.text];
}
}
return YES;
}
return YES;
}
网友评论