samuraisam
samuraisam

Reputation: 1947

NSNumberFormatter to format US Telephone Numbers

I'm trying to convert a string of numbers, entered by the user, into a sexy string like Phone.app on the iPhone. Here is the code I'm using, which doesn't work (no special format comes out) and after a certain number of digits it just starts adding "0" to the end of the string.

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterNoStyle];
[formatter setPositiveFormat:@"+# (###) ###-###"];
[formatter setLenient:YES];
NSString *strDigits = [self stringByReplacingOccurrencesOfRegex:@"[^0-9+]" withString:@""];
return [formatter stringFromNumber:[NSNumber numberWithDouble:[strDigits doubleValue]]];

Upvotes: 8

Views: 12055

Answers (5)

Mark T.
Mark T.

Reputation: 71

One thing that is never mentioned here is that a NSNumberFormatter will always convert the value into, well, a NSNumber. If you want a more generalized NSFormatter, subclass NSFormatter and implement the methods mentioned in the Creating a Custom Formatter.

Upvotes: 0

Dan J
Dan J

Reputation: 25673

I think your issue is that NSNumberFormatter does not support brackets, spaces or dashes. I tried to implement the same method as you and it failed silently and just output unformatted text.

The general problem here is that the iPhone SDK doesn't provide a method to format phone numbers in a locale dependent way.

I have raised bugs with Apple for the following (two of these were duplicates of known issues so I've included Apple's original bug # for those):

#6933244 - Need iPhone SDK interface to format text as locale dependent phone number
#5847381 - UIControl does not support a setFormatter method
#6024985 - Overridden UITextField drawTextInRect method is never called

In an ideal world Apple would provide an NSPhoneNumberFormatter, and you would call setFormatter on your UIControl so it displayed text in a nice pretty way. Unfortunately that doesn't exist on the iPhone.

Upvotes: 11

Stas
Stas

Reputation: 641

Apple says you can implement your custom formatter if existing formatters' functionality is not enough:

NSFormatter is intended for subclassing. A custom formatter can restrict the input and enhance the display of data in novel ways. For example, you could have a custom formatter that ensures that serial numbers entered by a user conform to predefined formats. Before you decide to create a custom formatter, make sure that you cannot configure the public subclasses NSDateFormatter and NSNumberFormatter to satisfy your requirements.

For instructions on how to create your own custom formatter, see Creating a Custom Formatter.

See the section in Data Formatting Guide

Upvotes: 2

Ahmed Abdelkader
Ahmed Abdelkader

Reputation: 1735

The UIPhoneFormats.plist contains predefined phone formats for each locale. So, if you're only interested in US phone numbers, you'll need to consider these formats:

+1 (###) ###-####
1 (###) ###-####
011 $
###-####
(###) ###-####

I had to do something similar, and I shared the results I got here: http://the-lost-beauty.blogspot.com/2010/01/locale-sensitive-phone-number.html

Upvotes: 9

Adrian Archer
Adrian Archer

Reputation: 2323

Well a phone number should be 10 characters(11 with the leading 1), so you should start by changing this:

[formatter setPositiveFormat:@"+# (###) ###-####"];

And speaking of the leading 1, you need to check for that too.

Upvotes: 2

Related Questions