在iPhone应用程序中以美国格式显示电话号码

时间:2010-01-27 12:53:29

标签: ios objective-c iphone phone-number

在我的iPhone应用中,我有textfield接受电话号码。我需要显示号码 在美国电话号码格式。这就像(000)000-0000。通常喜欢iPhone联系人电话 号码输入。我怎样才能做到这一点。任何想法都将不胜感激。

4 个答案:

答案 0 :(得分:3)

对于自动格式化,我将addTarget与我PhoneNumberFormatter Adam友好引用的here结合使用。该实现描述为{{3}}。

答案 1 :(得分:1)

您将从

获取电话号码格式化程序

http://the-lost-beauty.blogspot.com/2010/01/locale-sensitive-phone-number.html

不要忘记使用PhoneNumberFormatter类。该课程也适用于该博客

答案 2 :(得分:0)

由于用户在文本字段中手动输入电话号码,您可以使用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];
}

答案 3 :(得分:0)

- (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];
}
相关问题