iphone - How to format NSString into a phone number in the USA? -


I have a simple app that collects recruitment information in an event. A form field was for a phone number, and I wanted a simple way to format the phone number as a user type.

The phone number should be developed as a user type, so a sample output for a string of numbers should look like this:

  1 1 (20) 1 ( 206) 55 1 (206) 555-55 1 (206) 555-5555   

Or alternatively, if the user does not enter 1 before the area code, then the phone number in this way Will develop:

  (20) (206) 55 (206) 555-55 (206) 555-5555   

If the phone number is too long, So it just shows a plain string of numbers I should answer my own question as it is easy to do so, but stack overflow should be:

  20655555555555555   

There was no good answer.

What I have done here: UITFeld Delegate code textField: shouldChangeCharactersInRange: replacementString: The text of the method UITextField , and I have run a small method:

  - (zero ) UpdatePhonecumberwithstring: (NSMtring * string) {NSMutableString * finalString = [NSMutableString New]; NSMutableString * Workflow string = [NSMutableString string string]; If (if the phone string is available, the lamb;) // // If this statement refers to errors, when the user removes the last character in textfield (if the [[[functional phone string substringToIndex: 1] is EqualToString: @ "1 "]) {// If the user has typed" 1 "as the first digit, then it is a prefix before the field code. [Last string appendering: @ "1"]; [Editing the phone calling stringBackers: string with NSMakeRange (0, 1): @ ""]; } If working (phone string. Lip & lt; 3) {// if the user is dialing area code code ... [last string apnformat: [NSString stringWithFormat: @ "(% @)", working Hammerstinging]]; } And if (is working on the phone stringing lane & lt; 6) {// if the user is dial 3 points after area code ... [Last string application format: [NSString stringWithFormat: @ "(% @)% @ ", [Is working on the phonestine substringWithRange: NSMKengens (0, 3)], [Working Phone String Affirmation Index Index: 3]]]; } And if (is working, phone string. Lff & lt; 11) {// if the user is dial the last 4 digits of the phone number ... [Last string application format: [NSString stringWithFormat: @ "(% @) [Working Phonestrstring Originwindrangen: NSMCarange (0, 3)], [Working Phonestrating Orringwindrangen: NSMCarange (3, 3)], [Working Phonestrstring subdivision form index: 6]]]; } Else {// If the user is typed in a bunch of letters, just show the full string. Last string = phone string; } PhoneNumberField.text = Last string; } Else {// If the user has not changed the text field at all to the text ... phoneNumberField.text = @ ""; }}   

Hope this helps you!

Comments