Monday, August 20, 2012

iPhone SDK and phone number formatter

For all the wonderful things that iPhone SDK does for us beautifully, I finally found one feature that is not supported: phone number formatting. I was hopeful that after using NSNumberFormatterCurrencyStyle, I will be able to find something similar to format my phone numbers. I was no able to find any such a formatter. So I decided to use NSNumberFormatter with custom coding around it to get the job done:
  NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterNoStyle];
    [formatter setPositiveFormat:@"(###) ###-####"];
    [formatter setLenient:YES];
    
    homePhoneValue.text = [formatter stringFromNumber:[NSNumber numberWithDouble:[homeNumberAsString doubleValue]]];
    mobilePhoneValue.text = [formatter stringFromNumber:[NSNumber numberWithDouble:[mobileNumberAsString doubleValue]]];
Unfortunately, that did not work either. As it turns out, iPhone SDK formatter does not support spaces, dashes and brackets. If the above code is ran, it fails silently, and the text is displayed in its "pre-formatted" form. I think I read somewhere that there is a bug opened with iPhone Developers to fix this issue. Until then... 

I think I will write my own formatter...

No comments:

Post a Comment