Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/serheo/phonenumberformatter
UITextField subclass for formatting phone numbers. Allow different formats for different countries(patterns).
https://github.com/serheo/phonenumberformatter
formatter ios phone phone-format phone-formatter swift
Last synced: 6 days ago
JSON representation
UITextField subclass for formatting phone numbers. Allow different formats for different countries(patterns).
- Host: GitHub
- URL: https://github.com/serheo/phonenumberformatter
- Owner: Serheo
- License: mit
- Created: 2017-09-20T05:23:00.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-07-16T20:58:57.000Z (6 months ago)
- Last Synced: 2024-12-28T21:10:03.931Z (13 days ago)
- Topics: formatter, ios, phone, phone-format, phone-formatter, swift
- Language: Swift
- Homepage:
- Size: 34.2 KB
- Stars: 97
- Watchers: 4
- Forks: 27
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Phone Number Formatter
[![Build](https://travis-ci.org/Serheo/PhoneNumberFormatter.svg?branch=master)](https://travis-ci.org/Serheo/PhoneNumberFormatter)
![Swift](https://img.shields.io/badge/Swift-4.2-orange.svg)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)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
```swift
textField.config.defaultConfiguration = PhoneFormat(defaultPhoneFormat: "(###) ###-##-##")
```
All input strings will be parsed in that way.
Example: +1 (123) 123-45-67
### Prefix Format
You can set prefix on all inputs:
```swift
textField.config.defaultConfiguration = PhoneFormat(defaultPhoneFormat: "(###) ###-##-##")
textField.prefix = "+7 "
```### Multiple Formats
```swift
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
```swift
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
```swift
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.