A Swift framework for formatting text input on iOS, macOS and tvOS.
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:
- consistent Swift API on iOS, macOS and tvOS;
- built-in formats:
- capability to add custom formats;
- total code coverage.
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.
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 toformat
(in this case, according to the bank card number format); - updates
textInputBinding.value
when text intextField
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.
textInputBinding.eventHandler = { [unowned self] event in
switch event {
case let .editingChanged(state, changes):
if changes.contains(.value) {
// Use `state.value`
}
default:
break
}
}
- iOS 8.0 / macOS 10.10 / tvOS 9.0
- Xcode 8.0
- Swift 3.0
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.
TextInputKit is released under the MIT license. See LICENSE for details.