开发者

Displaying phone number in US format in iPhone application

In my iPhone app I have a textfield to accept phone number. I need to display the number in US Phon开发者_如何学Pythone number format. That is like (000) 000-0000. Typically like iPhone Contact phone number entry. How can I do this. Any idea will be greatly appreciated.


For auto-formatting, I used addTarget in combination with my PhoneNumberFormatter that Adam kindly referenced. The implementation is described here.


You will get the phone number formatter from

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

Don't forget to use the PhoneNumberFormatter class. This class is also availabel in that blog


Since the user is entering the phone number manually in the text field, you can use the UITextFieldDelegate Method textField:shouldChangeCharactersInRange:replacementString: to dynamically change the entered phone number by adding '(' before the 1st digit ')' after 3 digits are entered '-' after 6 digits. (or) after the number entry is done, you can change the displayed format like this:

- (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];
}


- (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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜