在iPhone应用程序中显示美国格式的电话号码
问题描述:
在我的iPhone应用程序中,我有一个textfield
接受电话号码。我需要以美国电话号码格式显示号码 。这就像(000)000-0000。通常喜欢iPhone联系电话 号码输入。我怎样才能做到这一点。任何想法将不胜感激。在iPhone应用程序中显示美国格式的电话号码
答
由于用户在文本字段中手动输入电话号码,因此可以使用UITextFieldDelegate方法textField:shouldChangeCharactersInRange:replacementString:
通过在输入3位数字后在第1位数字'''前添加'('''来动态更改输入的电话号码' - '6位数后。 (或)号码输入完成后,您可以更改显示的格式是这样的:
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
//resign the keypad and check if 10 numeric digits entered...
NSRange range;
range.length = 3;
range.location = 3;
textField.text = [NSString stringWithFormat:@"(%@)%@-%@", [textField.text substringToIndex:3], [textField.text substringWithRange:range], [textField.text substringFromIndex:6];
}
答
你将得到一个电话号码格式从
http://the-lost-beauty.blogspot.com/2010/01/locale-sensitive-phone-number.html
不要忘记使用PhoneNumberFormatter类。此课程还可在该博客中获得
答
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
//resign the keypad and check if 10 numeric digits entered...
NSRange range;
range.length = 3;
range.location = 3;
textField.text = [NSString stringWithFormat:@"(%@) %@-%@", [textField.text substringToIndex:3], [textField.text substringWithRange:range], [textField.text substringFromIndex:6];
}