Latest0.1.5
Homepagehttps://github.com/luximetr/LightTextInput
LicenseMIT
Platformsios 8.0
Authors

CI Status
Version
License
Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • iOS 8.0+

Installation

LightTextInput is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'LightTextInput'

Usage

Import

import LightTextInput

Formatting with TextFormatter

let phoneFormatter = TextFormatter(textPattern: "### (###) ###-##-##")
phoneFormatter.formattedText(from: "+123456789012") // +12 (345) 678-90-12

let customFormatter = TextFormatter(textPattern: "###-###custom###-###")
customFormatter.formattedText(from: "111222333444") // 111-222custom333-444

You can also set own symbol in pattern

let cardFormatter = TextFormatter(textPattern: "XXXX XXXX XXXX XXXX", patternSymbol: "X")
cardFormatter.formattedText(from: "4444555566667777") // 4444 5555 6666 7777

For string with different length

let formatter = TextFormatter(textPattern: "## ###-##")
formatter.formattedText(from: "1234") // 12 34
formatter.formattedText(from: "123456789") // 12 345-67

Unformatting

let formatter = TextFormatter(textPattern: "## ###-##")
formatter.unformattedText(from: "99 888-77") // 9988877

Formatting during typing

For formatting during typing is necessary to create TextInputController instance with formatter. You need to implement TextInput protocol in your own UITextField/UITextView/someone else or use ready solutions (TextInputField/TextInputView) by subclassing. It need to set controllers’s textInput property.

let textInputController = TextInputController()

let textInput = TextInputField() // or TextInputView or any TextInput
textInputController.textInput = textInput // setting textInput

let formatter = TextInputFormatter(textPattern: "### (###) ###-##-##", prefix: "+12")
textInputController.formatter = formatter // setting formatter

Controller listen shouldChangeTextIn delegate method. But you can also add more than one delegate if needed. Methods of delegates, that should return Bool value gather with && operator. Such if one of delegates return false, that meens textInput will receive false. If you want to receive true to textInput, all delegates must return true.

You can set allowedSymbolsRegex to formatter to filter input symbols with regex. All symbols, that satisfy to RegEx will be able to typing in textInput.
This property only applies to inputed symbols from keybord, but not for prefix.

inputFieldFormatter.allowedSymbolsRegex = "[0-9]" // allowed only numbers

Attributes for range

To set attributes for string at range use addAttributes(_, range) method for textInput.

textInput.addAttributes([.foregroundColor : UIColor.lightGray], range: NSRange(location: 0, length: 3))

Author

luximetr, [email protected]

License

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

Latest podspec

{
    "name": "LightTextInput",
    "version": "0.1.5",
    "summary": "Pod for using textFields and textViews with common interface",
    "description": "Implemented TextInput protocol and TextInputField with TextInputView, that implements this protocol. This allow use textField or textView with TextInput protocol.",
    "homepage": "https://github.com/luximetr/LightTextInput",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "luximetr": "[email protected]"
    },
    "source": {
        "git": "https://github.com/luximetr/LightTextInput.git",
        "tag": "0.1.5"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "LightTextInput/Classes/**/*",
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This