I have a simple app that helps recruiters collect information while at an event. One form field was for a phone number, and I wanted a simple way to reformat the phone number as the user types.
The phone number should evolve as the user types, so 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 didn’t enter a 1 before the area code, the phone number would evolve like this:
(20)
(206) 55
(206) 555-55
(206) 555-5555
If the phone number is too long, then it should just show a plain string of numbers:
20655555555555555
Here’s what I did: the
UITextFieldDelegatehandles thetextField:shouldChangeCharactersInRange :replacementString:method by getting the text of theUITextField, and running a little method that I wrote:Hope this helps you!