Skip to content

artyom-stv/TextInputKit

Repository files navigation

Build Status Carthage compatible

TextInputKit

A Swift framework for formatting text input on iOS, macOS and tvOS.

Overview

While developing an application, you may encounter a task to format text input of bank card information, phone numbers, etc. TextInputKit strives to help you solve this task efficiently.

TextInputKit is a framework with:

Installation

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 TextInputKit into your Xcode project using Carthage, specify it in your Cartfile:

github "artyom-stv/TextInputKit"

Run carthage update to build the framework and drag the built TextInputKit.framework into your Xcode project.

Usage

Bind a Format to a Text Field

First, a TextInputFormat is created (see how to create a built-in format or a custom format).

let format = TextInputFormats.bankCardNumber() // -> `TextInputFormat<BankCardNumber>`

To drive text input formatting, a TextInputBinding is created.

let textInputBinding = format.bind(to: textField) // -> `TextInputBinding<BankCardNumber>`

If you don't need to reuse a format, the code can be shortened to the following:

let textInputBinding = TextInputFormats.bankCardNumber().bind(to: textField) // -> `TextInputBinding<BankCardNumber>`

textInputBinding performs the following functions:

  • formats text input in textField according to format (in this case, according to the bank card number format);
  • updates textInputBinding.value when text in textField changes;
  • notifies of the events.

The functions mentioned above are performed only while textInputBinding is bound to textField:

  • textField is alive;
  • textInputBinding is alive;
  • textInputBinding.unbind() isn't called.

Subscribe to Text Input Events

textInputBinding.eventHandler = { [unowned self] event in
    switch event {
    case let .editingChanged(state, changes):
        if changes.contains(.value) {
            // Use `state.value`
        }
    default:
        break
    }
}

Requirements

  • iOS 8.0+ / macOS 10.10+ / tvOS 9.0+
  • Xcode 8.0+
  • Swift 3.0+

Framework Evolution

There are plans to extend the set of built-in formats.

Please, create an issue or comment an existing issue, if you need support of a new format.

Examples of feasible formats:

  • Pattern-based Format;
  • Decimal Number;
  • E-mail.

License

TextInputKit is released under the MIT license. See LICENSE for details.

About

Text Input Formatting for iOS and macOS

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages