iosswiftuitextfieldphone-numbernsformatter

Formatting as Phone Number on Custom UITextField while typing


I have custom UITextFields that I created using TextFieldEffects. Now I have a different challenge that I need to use phone number formatting for some instances of TextFieldEffects but not all. At this point, I've found PhoneNumberKit, which seems to be doing exactly what I want to do.

However, TextFieldEffects is an instance of UITextField that can be seen here, thus if I change UITextField to PhoneNumberTextField on line 23, it will affect all of the TextFieldEffects.

Is there a way I can combine these two libraries and be able to get some fields to be affected by PhoneNumberKit but not all?


Or is there any better / more convenient way (maybe any other library or without using external library) to achieve what I want to achieve? What other options do I have? What is the best approach on this case?


For further discussion, I asked another question here..


Solution

  • I think if you look through the documentation and delegate methods of both of your libraries, you should be able to integrate the two. Without doing all the work, you should use UITextFieldDelegate to tell you when text has changed, and then use the formatter methods of PhoneNumberKit to take that raw string and update the contents of your custom TextField.