Latest 0.98
Homepage https://github.com/alucarders/SHSPhoneComponentSwift
License MIT
Platforms ios 9.3, requires ARC
Authors

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.

Installation

CocoaPods

To install SHSPhoneComponentSwift using CocoaPods just add the following line to your Podfile

pod 'SHSPhoneComponentSwift'

Carthage

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.

How To

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

Default format

phoneField.formatter.setDefaultOutputPattern("+# (###) ###-##-##")

SHSPhoneComponentSwift

All input strings will be parsed in that way. Example: +7 (920) 123-45-67

Prefix Format

You can set prefix for all inputs:

phoneField.formatter.setDefaultOutputPattern("(###) ###-##-##")
phoneField.formatter.prefix = "+7 "

SHSPhoneComponentSwift

Multiple Formats

phoneField.formatter.prefix = nil
phoneField.formatter.addOutputPattern("+# (###) ###-##-##", 
                                      forRegExp: "^7[0-689]\d*$",
                                      imagePath: "flag_ru")
phoneField.formatter.addOutputPattern("+### ###-##-##", 
                                      forRegExp: "^380\d*$", 
                                      imagePath: "flag_ua")

SHSPhoneComponentSwift

Multiple Formats with prefix

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")

Specific Formats

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")

Formatting

If you need only formatting function you can use SHSPhoneNumberFormatter class.

Requirements

iOS 9.3+

License

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.

Latest podspec

{
    "name": "SHSPhoneComponentSwift",
    "version": "0.98",
    "summary": "UITextField and NSFormatter subclasses for formatting phone numbers. Allow different formats for different countries(patterns)",
    "homepage": "https://github.com/alucarders/SHSPhoneComponentSwift",
    "license": "MIT",
    "authors": {
        "Max Kupetskii": "[email protected]"
    },
    "platforms": {
        "ios": "9.3"
    },
    "source": {
        "git": "https://github.com/alucarders/SHSPhoneComponentSwift.git",
        "tag": "0.98"
    },
    "source_files": "SHSPhoneComponent/Library/*.{h,swift}",
    "requires_arc": true,
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This