In my app I am taking phone number as an input from user. Number should be in US format. I want to display it like (555)-888-888 dynamically. For example when user starts to input number when he reaches to 4 digit it shows number like this (555)-4 and so on. I tried to replaceString method but i found that it will not work.
Look at NBAsYouTypeFormatter
class of libPhoneNumber-iOS library.
You create new instance of NSAsYouTypeFormatter
with your US region code given:
NBAsYouTypeFormatter *asYouTypeFormatter = [[NBAsYouTypeFormatter alloc] initWithRegionCode:REGION_CODE_STRING];
Then every time user changes the phone number you call:
- (NSString*)inputDigit:(NSString*)nextChar;
or
- (NSString*)removeLastDigit;
Returned NSString
from this two methods is your dynamically formatted phone number.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With