UITextField and NSFormatter subclasses for formatting phone numbers. Allow different formats for different countries(patterns). Caret positioning works excellent.
Swift 4.
If you need ObjC support use - https://github.com/Serheo/SHSPhoneComponent/
github "Serheo/PhoneNumberFormatter"
pod "SwiftPhoneNumberFormatter"
textField.config.defaultConfiguration =PhoneFormat(defaultPhoneFormat:"(###) ###-##-##")All input strings will be parsed in that way. Example: +1 (123) 123-45-67
You can set prefix on all inputs:
textField.config.defaultConfiguration =PhoneFormat(defaultPhoneFormat:"(###) ###-##-##")
textField.prefix ="+7 "textField.config.defaultConfiguration =PhoneFormat(defaultPhoneFormat:"##########")
textField.prefix =nilletcustom1=PhoneFormat(phoneFormat:"+# (###) ###-##-##", regexp:"^7[0-689]\\d*$")
textField.config.add(format: custom1)letcustom2=PhoneFormat(phoneFormat:"+### ###-##-##", regexp:"^380\\d*$")
textField.config.add(format: custom2)textField.config.defaultConfiguration =PhoneFormat(defaultPhoneFormat:"### ### ###")
textField.prefix ="+7 "letcustom1=PhoneFormat(phoneFormat:"(###) ###-##-##", regexp:"^1\\d*$")
textField.config.add(format: custom1)letcustom2=PhoneFormat(phoneFormat:"(###) ###-###", regexp:"^2\\d*$")
textField.config.add(format: custom2)iOS 9+ Swift 4
PhoneNumberFormatter is available under the MIT license. See the LICENSE file for more info.