我使用了类似的东西,效果很好,格式是 (###) ###-#### x###... 这可能不是最高效的过程,但从所有意图和目的来看,它都做得很好。
假设您有一个 textField 的属性/实例变量,以及一个 BOOL 来跟踪您是否希望允许它被格式化,这就是我实现它的方式。当我为电话号码创建我的 textField 时,我执行以下操作:
self.textField.keyboardType = UIKeyboardTypeNumberPad;
self.textField.delegate = self;
[self.textField addTarget:self action:@selector(formatPhoneNumber) forControlEvents:UIControlEventEditingChanged];
上面将键盘变成了数字键盘,每次更改值时我们调用 formatPhoneNumber,还要确保实现 UITextFieldDelegateProtocol 并将自己设置为字段的委托。
然后,我们实现 shouldChangeCharactersInRange 方法,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// this is what the textField's string will be after changing the characters
NSString *resultString = [textField.text stringByReplacingCharactersInRange:range withString:string];
// flag that we should attempt to format the phone number ONLY if they are adding
// characters, otherwise if they are deleting characters we simply want to allow
// them to delete them and not format them
_shouldAttemptFormat = resultString.length > self.textField.text.length;
return YES;
}
format phone number 方法只会在我们确定应该在 shouldChangeCharactersInRange 方法中格式化数字时尝试格式化数字,将当前文本分解成有用的电话号码片段,例如 (555) 444-2222 -> 5554442222,然后很好地格式化它:
- (void)formatPhoneNumber {
// this value is determined when textField shouldChangeCharactersInRange is called on a phone
// number cell - if a user is deleting characters we don't want to try to format it, otherwise
// using the current logic below certain deletions will have no effect
if (!_shouldAttemptFormat) {
return;
}
// here we are leveraging some of the objective-c NSString functions to help parse and modify
// the phone number... first we strip anything that's not a number from the textfield, and then
// depending on the current value we append formatting characters to make it pretty
NSString *currentString = self.textField.text;
NSString *strippedValue = [currentString stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, currentString.length)];
NSString *formattedString;
if (strippedValue.length == 0) {
formattedString = @"";
}
else if (strippedValue.length < 3) {
formattedString = [NSString stringWithFormat:@"(%@", strippedValue];
}
else if (strippedValue.length == 3) {
formattedString = [NSString stringWithFormat:@"(%@) ", strippedValue];
}
else if (strippedValue.length < 6) {
formattedString = [NSString stringWithFormat:@"(%@) %@", [strippedValue substringToIndex:3], [strippedValue substringFromIndex:3]];
}
else if (strippedValue.length == 6) {
formattedString = [NSString stringWithFormat:@"(%@) %@-", [strippedValue substringToIndex:3], [strippedValue substringFromIndex:3]];
}
else if (strippedValue.length <= 10) {
formattedString = [NSString stringWithFormat:@"(%@) %@-%@", [strippedValue substringToIndex:3], [strippedValue substringWithRange:NSMakeRange(3, 3)], [strippedValue substringFromIndex:6]];
}
else if (strippedValue.length >= 11) {
formattedString = [NSString stringWithFormat:@"(%@) %@-%@ x%@", [strippedValue substringToIndex:3], [strippedValue substringWithRange:NSMakeRange(3, 3)], [strippedValue substringWithRange:NSMakeRange(6, 4)], [strippedValue substringFromIndex:10]];
}
self.textField.text = formattedString;
}
下面是它的视频:
https://www.dropbox.com/s/xq9vb9p62oamova/phone.mov?dl=0
如果您想参考更多与此相关的代码,我在处理 iOS 表单的开源项目中使用了类似的东西。第一个链接是带有演示的 gif,因此您可以看到它的实际效果。
https://raw.githubusercontent.com/mamaral/MAFormViewController/master/Screenshots/form_demo.gif
https://github.com/mamaral/MAFormViewController