问题描述
我想知道如何格式化我用于电话号码的文本字段(例如 iPhone 上的添加新联系人"页面.当我输入新手机时,例如 1236890987,它会对其进行格式化如 (123) 689-0987.)我已经将键盘设置为数字键盘.
I was wondering how I can format the textField that I'm using for a phone number (ie like the "Add New Contact" page on the iPhone. When I enter in a new mobile phone, ex. 1236890987 it formats it as (123) 689-0987.)I already have the keyboard set as the number pad.
推荐答案
这是我的解决方案.. 效果很好!实时格式化电话号码.注意:这适用于 10 位电话号码.目前它会自动将其格式化为 (xxx) xxx-xxxx .. 调整到您心中的喜悦.
Here is my solution.. works great! Formats the phone number in realtime. Note: This is for 10 digit phone numbers. And currently it auto formats it like (xxx) xxx-xxxx.. tweak to your hearts delight.
首先在您的 shouldChangeCharactersInRange
中,您希望收集电话文本字段的整个字符串并将其传递给验证/格式化函数.
First in your shouldChangeCharactersInRange
you want to gather the whole string for the phone text field and pass it to the validation/formatting function.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString* totalString = [NSString stringWithFormat:@"%@%@",textField.text,string];
// if it's the phone number textfield format it.
if(textField.tag==102 ) {
if (range.length == 1) {
// Delete button was hit.. so tell the method to delete the last char.
textField.text = [self formatPhoneNumber:totalString deleteLastChar:YES];
} else {
textField.text = [self formatPhoneNumber:totalString deleteLastChar:NO ];
}
return false;
}
return YES;
}
这里是电话号码的格式.正则表达式可能会被清理一下.但是我已经测试了这段代码一段时间并且似乎通过了所有铃声.请注意,我们还使用此功能删除电话号码中的号码.在这里工作更容易一些,因为我们已经去除了所有其他非数字.
And here is where the phone number is formatted. The regex could probably be cleaned up a bit. But I have tested this code for a while and seems to pass all bells. Notice we also use this function to delete a number in the phone number. Works a little easier here because we already stripped out all the other non digits.
-(NSString*) formatPhoneNumber:(NSString*) simpleNumber deleteLastChar:(BOOL)deleteLastChar {
if(simpleNumber.length==0) return @"";
// use regex to remove non-digits(including spaces) so we are left with just the numbers
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[\s-\(\)]" options:NSRegularExpressionCaseInsensitive error:&error];
simpleNumber = [regex stringByReplacingMatchesInString:simpleNumber options:0 range:NSMakeRange(0, [simpleNumber length]) withTemplate:@""];
// check if the number is to long
if(simpleNumber.length>10) {
// remove last extra chars.
simpleNumber = [simpleNumber substringToIndex:10];
}
if(deleteLastChar) {
// should we delete the last digit?
simpleNumber = [simpleNumber substringToIndex:[simpleNumber length] - 1];
}
// 123 456 7890
// format the number.. if it's less then 7 digits.. then use this regex.
if(simpleNumber.length<7)
simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\d{3})(\d+)"
withString:@"($1) $2"
options:NSRegularExpressionSearch
range:NSMakeRange(0, [simpleNumber length])];
else // else do this one..
simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\d{3})(\d{3})(\d+)"
withString:@"($1) $2-$3"
options:NSRegularExpressionSearch
range:NSMakeRange(0, [simpleNumber length])];
return simpleNumber;
}
这篇关于电话号码的 UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!