Latest 1.5
Homepage https://github.com/Serheo/PhoneNumberFormatter
License MIT
Platforms ios 9.0, requires ARC
Authors

Phone Number Formatter

Build
Swift
Carthage compatible

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/

Installation

Carthage

github "Serheo/PhoneNumberFormatter"

CocoaPods

pod "SwiftPhoneNumberFormatter"

Getting Started

Default Format

textField.config.defaultConfiguration = PhoneFormat(defaultPhoneFormat: "(###) ###-##-##")

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

Image

Prefix Format

You can set prefix on all inputs:

textField.config.defaultConfiguration = PhoneFormat(defaultPhoneFormat: "(###) ###-##-##")
textField.prefix = "+7 "

Multiple Formats

textField.config.defaultConfiguration = PhoneFormat(defaultPhoneFormat: "##########")
textField.prefix = nil
let custom1 = PhoneFormat(phoneFormat: "+# (###) ###-##-##", regexp: "^7[0-689]\d*$")
textField.config.add(format: custom1)

let custom2 = PhoneFormat(phoneFormat: "+### ###-##-##", regexp: "^380\d*$")
textField.config.add(format: custom2)

Multiple Formats with prefix

textField.config.defaultConfiguration = PhoneFormat(defaultPhoneFormat: "### ### ###")
textField.prefix = "+7 "

let custom1 = PhoneFormat(phoneFormat: "(###) ###-##-##", regexp: "^1\d*$")
textField.config.add(format: custom1)

let custom2 = PhoneFormat(phoneFormat: "(###) ###-###", regexp: "^2\d*$")
textField.config.add(format: custom2)

Listening to changes

To be notified of changes on the textField input add a textDidChangeBlock closure

textField.textDidChangeBlock = { field in
  if let text = field?.text, text != "" {
      print(text)
  } else {
      print("No text")
  }

Attempting to listen to changes through other means will likely fail (e.g., implementing UITextFieldDelegate‘s textField:shouldChangeCharactersIn:range:).

Requirements

iOS 9+
Swift 4

License

PhoneNumberFormatter is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "SwiftPhoneNumberFormatter",
    "version": "1.5",
    "summary": "PhoneNumberFormatter for iOS",
    "homepage": "https://github.com/Serheo/PhoneNumberFormatter",
    "license": "MIT",
    "authors": {
        "Sergei Shatunov": "[email protected]"
    },
    "source": {
        "git": "https://github.com/Serheo/PhoneNumberFormatter.git",
        "tag": "1.5"
    },
    "platforms": {
        "ios": "9.0"
    },
    "source_files": "PhoneNumberFormatter/Sources/**/*.{swift}",
    "requires_arc": true
}

Pin It on Pinterest

Share This