格式化美国电话
by 伍雪颖
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSString *toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; if (_phoneTextfield.isEditing) { // format american phonenum (021) 456-8901 if (toBeString.length == 1 && range.length == 0) { [textField setText:[NSString stringWithFormat:@"(%@",toBeString]]; return NO; }else if (toBeString.length == 5 && range.length == 0) { [textField setText:[NSString stringWithFormat:@"%@) %@",[toBeString substringToIndex:4],string]]; return NO; }else if (toBeString.length == 10 && range.length == 0) { [textField setText:[NSString stringWithFormat:@"%@-%@",[toBeString substringToIndex:9],string]]; return NO; }else if (toBeString.length == 15 && range.length == 0) { SHAKEPHONE; return NO; }else if (toBeString.length == 6 && range.length == 1) { [textField setText:[toBeString substringToIndex:4]]; return NO; }else if (toBeString.length == 10 && range.length == 1) { [textField setText:[toBeString substringToIndex:9]]; return NO; }else if (toBeString.length == 1 && range.length == 1) { [textField setText:@""]; return NO; } } return YES; }
时间: 2024-11-01 15:56:25