A Swift version of SHSPhoneComponent framework. UITextField and Formatter subclasses for formatting phone numbers. Allows to use different formats for different countries (patterns). The caret positioning works excellent.
To install SHSPhoneComponentSwift using CocoaPods just add the following line to your Podfile
pod 'SHSPhoneComponentSwift'
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. You can install Carthage with HomeBrew using the following command:
$ brew update
$ brew install carthage
To integrate SHSPhoneComponentSwift into your Xcode project using Carthage, specify it in your Cartfile
:
github "alucarders/SHSPhoneComponentSwift"
Run carthage update
to build the framework and drag the built SHSPhoneComponentSwift.framework
into your Xcode project.
After adding the framework to your project, you need to import the module
import SHSPhoneComponentSwift
After this you can start using SHSPhoneTextField
type for displaying phone numbers
phoneField.formatter.setDefaultOutputPattern("+# (###) ###-##-##")
All input strings will be parsed in that way. Example: +7 (920) 123-45-67
You can set prefix for all inputs:
phoneField.formatter.setDefaultOutputPattern("(###) ###-##-##")
phoneField.formatter.prefix = "+7 "
phoneField.formatter.prefix = nil
phoneField.formatter.addOutputPattern("+# (###) ###-##-##",
forRegExp: "^7[0-689]\\d*$",
imagePath: "flag_ru")
phoneField.formatter.addOutputPattern("+### ###-##-##",
forRegExp: "^380\\d*$",
imagePath: "flag_ua")
phoneField.formatter.setDefaultOutputPattern("### ### ###")
phoneField.formatter.prefix = "+7 "
phoneField.formatter.addOutputPattern("(###) ###-##-##",
forRegExp: "^1\\d*$",
imagePath:"flag_ru")
phoneField.formatter.addOutputPattern("(###) ###-###",
forRegExp: "^2\\d*$",
imagePath:"flag_ua")
If you want to format some numbers in a specific way just do
phoneField.formatter.addOutputPattern("+# (###) ###-##-##",
forRegExp: "^7[0-689]\\d*$",
imagePath: "flag_ru")
phoneField.formatter.addOutputPattern("+### (##) ###-###",
forRegExp: "^374\\d*$",
imagePath: "flag_am")
If you need only formatting function you can use SHSPhoneNumberFormatter class.
iOS 9.3+
SHSPhoneComponentSwift is available under the MIT license. See the LICENSE file for more info. All credits go to the author of the original framework SHSPhoneComponent.