1. 创建两个全局变量 遵守UITextField 的代理
NSString *_previousTextFieldContent;
UITextRange *_previousSelection;
2.给输入银行卡号的textField 添加事件 设置代理
_bankCardNumTextField.delegate=self
[_bankCardNumTextField addTarget:self action:@selector(reformatAsBankCardNumber:) forControlEvents:UIControlEventEditingChanged];
3.在你的.m文件里粘贴上下面代码
#pragma mark - 重新格式化银行卡号
-(void)reformatAsBankCardNumber:(UITextField *)textField
{
// 判断正确的光标位置
NSUInteger targetCursorPostion = [textField offsetFromPosition:textField.beginningOfDocument toPosition:textField.selectedTextRange.start];
// 没有插入空格的银行卡号
NSString *bankCardNumberWithoutSpaces = [self removeNonDigits:textField.text andPreserveCursorPosition:&targetCursorPostion];
// 避免超过19位的输入
if(bankCardNumberWithoutSpaces.length > 19) {
[textField setText:_previousTextFieldContent];
textField.selectedTextRange = _previousSelection;
return;
}
//获取插入空格后的银行卡号
NSString *bankCardNumberWithSpaces = [self insertSpacesEveryFourDigitsIntoString:bankCardNumberWithoutSpaces andPreserveCursorPosition:&targetCursorPostion];
textField.text = bankCardNumberWithSpaces;
UITextPosition *targetPostion = [textField positionFromPosition:textField.beginningOfDocument offset:targetCursorPostion];
[textField setSelectedTextRange:[textField textRangeFromPosition:targetPostion toPosition:targetPostion]];
}
#pragma mark - 除去非数字字符(空格),确定光标正确位置
// string:当前的string cursorPosition:光标位置 return 处理过后的string
- (NSString *)removeNonDigits:(NSString *)string andPreserveCursorPosition:(NSUInteger *)cursorPosition
{
NSUInteger originalCursorPosition =*cursorPosition;
NSMutableString *digitsOnlyString = [NSMutableString new];
for (NSUInteger i=0; i<string.length; i++) {
unichar characterToAdd = [string characterAtIndex:i];
if(isdigit(characterToAdd)) {
NSString *stringToAdd = [NSString stringWithCharacters:&characterToAdd length:1];
[digitsOnlyString appendString:stringToAdd];
}
else {
if(i<originalCursorPosition) {
(*cursorPosition)--;
}
}
}
return digitsOnlyString;
}
#pragma mark - 将空格插入我们现在的string 中,并确定光标的正确位置,防止在空格中出现问题
//string:当前的string cursorPosition:光标位置 return:处理后有空格的string
- (NSString *)insertSpacesEveryFourDigitsIntoString:(NSString *)string andPreserveCursorPosition:(NSUInteger *)cursorPosition
{
NSMutableString *stringWithAddedSpaces = [NSMutableString new];
NSUInteger cursorPositionInSpacelessString = *cursorPosition;
for (NSUInteger i=0; i<string.length; i++) {
if ( i > 0 )
{
if(i%4 == 0) {
[stringWithAddedSpaces appendString:@" "];
if(i < cursorPositionInSpacelessString) {
(*cursorPosition)++;
}
}
}
unichar characterToAdd = [string characterAtIndex:i];
NSString *stringToAdd = [NSString stringWithCharacters:&characterToAdd length:1];
[stringWithAddedSpaces appendString:stringToAdd];
}
return stringWithAddedSpaces;
}
#pragma mark - UITextFieldDelegate
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
_previousSelection = textField.selectedTextRange;
_previousTextFieldContent = textField.text;
return YES;
}