Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phone number format ios

Tags:

ios

nsstring

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.

like image 687
prajapatijayesh Avatar asked Nov 30 '22 19:11

prajapatijayesh


1 Answers

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.

like image 181
damirstuhec Avatar answered Dec 10 '22 23:12

damirstuhec