1 @interface ViewController ()<UITextFieldDelegate> 2 { 3 NSString *previousTextFieldContent; 4 UITextRange *previousSelection; 5 } 6 @end
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 // Do any additional setup after loading the view, typically from a nib. 5 self.textField.delegate = self; 6 [self.textField addTarget:self action:@selector(formatPhoneNumber:) forControlEvents:UIControlEventEditingChanged]; 7 }
1 - (void)formatPhoneNumber:(UITextField*)textField 2 { 3 NSUInteger targetCursorPosition = 4 [textField offsetFromPosition:textField.beginningOfDocument 5 toPosition:textField.selectedTextRange.start]; 6 NSLog(@"targetCursorPosition:%li", (long)targetCursorPosition); 7 // nStr表示不带空格的号码 8 NSString* nStr = [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""]; 9 NSString* preTxt = [previousTextFieldContent stringByReplacingOccurrencesOfString:@" " 10 withString:@""]; 11 12 char editFlag = 0;// 正在执行删除操作时为0,否则为1 13 14 if (nStr.length <= preTxt.length) { 15 editFlag = 0; 16 } 17 else { 18 editFlag = 1; 19 } 20 21 // textField设置text 22 if (nStr.length > 11) 23 { 24 textField.text = previousTextFieldContent; 25 textField.selectedTextRange = previousSelection; 26 return; 27 } 28 29 // 空格 30 NSString* spaceStr = @" "; 31 32 NSMutableString* mStrTemp = [NSMutableString new]; 33 int spaceCount = 0; 34 if (nStr.length < 3 && nStr.length > -1) 35 { 36 spaceCount = 0; 37 }else if (nStr.length < 7 && nStr.length >2) 38 { 39 spaceCount = 1; 40 41 }else if (nStr.length < 12 && nStr.length > 6) 42 { 43 spaceCount = 2; 44 } 45 46 for (int i = 0; i < spaceCount; i++) 47 { 48 if (i == 0) { 49 [mStrTemp appendFormat:@"%@%@", [nStr substringWithRange:NSMakeRange(0, 3)], spaceStr]; 50 }else if (i == 1) 51 { 52 [mStrTemp appendFormat:@"%@%@", [nStr substringWithRange:NSMakeRange(3, 4)], spaceStr]; 53 }else if (i == 2) 54 { 55 [mStrTemp appendFormat:@"%@%@", [nStr substringWithRange:NSMakeRange(7, 4)], spaceStr]; 56 } 57 } 58 59 if (nStr.length == 11) 60 { 61 [mStrTemp appendFormat:@"%@%@", [nStr substringWithRange:NSMakeRange(7, 4)], spaceStr]; 62 } 63 64 if (nStr.length < 4) 65 { 66 [mStrTemp appendString:[nStr substringWithRange:NSMakeRange(nStr.length-nStr.length % 3, 67 nStr.length % 3)]]; 68 }else if(nStr.length > 3) 69 { 70 NSString *str = [nStr substringFromIndex:3]; 71 [mStrTemp appendString:[str substringWithRange:NSMakeRange(str.length-str.length % 4, 72 str.length % 4)]]; 73 if (nStr.length == 11) 74 { 75 [mStrTemp deleteCharactersInRange:NSMakeRange(13, 1)]; 76 } 77 } 78 NSLog(@"=======mstrTemp=%@",mStrTemp); 79 80 textField.text = mStrTemp; 81 // textField设置selectedTextRange 82 NSUInteger curTargetCursorPosition = targetCursorPosition;// 当前光标的偏移位置 83 if (editFlag == 0) 84 { 85 //删除 86 if (targetCursorPosition == 9 || targetCursorPosition == 4) 87 { 88 curTargetCursorPosition = targetCursorPosition - 1; 89 } 90 } 91 else { 92 //添加 93 if (nStr.length == 8 || nStr.length == 3) 94 { 95 curTargetCursorPosition = targetCursorPosition + 1; 96 } 97 } 98 99 UITextPosition *targetPosition = [textField positionFromPosition:[textField beginningOfDocument] 100 offset:curTargetCursorPosition]; 101 [textField setSelectedTextRange:[textField textRangeFromPosition:targetPosition 102 toPosition :targetPosition]]; 103 } 104 105 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 106 { 107 previousTextFieldContent = textField.text; 108 previousSelection = textField.selectedTextRange; 109 110 return YES; 111 }
时间: 2024-10-19 09:54:13