https://github.com/alozavr/dginputfield
DGInputField. Formattable and validatable UITextField with care of cursor position
https://github.com/alozavr/dginputfield
caret-position ios swift textfield textformatter validations
Last synced: 2 months ago
JSON representation
DGInputField. Formattable and validatable UITextField with care of cursor position
- Host: GitHub
- URL: https://github.com/alozavr/dginputfield
- Owner: Alozavr
- License: mit
- Created: 2018-01-31T07:26:03.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-02-03T08:28:39.000Z (over 7 years ago)
- Last Synced: 2025-01-20T09:08:34.317Z (4 months ago)
- Topics: caret-position, ios, swift, textfield, textformatter, validations
- Language: Swift
- Homepage:
- Size: 32.2 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# DGInputField
Hi! This is a simple `UITextField` sublass that handles formatting, input validation and cursor position for you!
# Installation
Just put `DGInputField` folder into your project!# Usage
You can just make your textfield of `DGInputField` class in your storyboard or just create it programmatically
`DGInputField` takes delegation of `UITextField` that's why you should not override `delegate` property. Another delegate is provided:
```swift
public protocol DGInputFieldDelegate: UITextFieldDelegate {
func textDidChange(in textField: DGInputField, formattedText: String, unformattedText: String)
}
```
If you do not specify formatter, then `formattedText` will be equal to `unformattedText`# Formatting
To add formatting for your input you should implement `StringFormatterProtocol` :
```swift
public protocol StringFormatterProtocol {
func format(_ string: String) -> String
func removeFormat(_ string: String) -> String
}
```
You can look an example in demo project. There is `CardNumberFormatter` to format input in groups of 4
# ValidatingFor now validation is made to prevent incorrect input. Perhaps it will be reworked in future. Just like formatting to support validation you just implement protocol `InputStringValidatorProtocol`:
```swift
public protocol InputStringValidatorProtocol {
func isValid(string: String) -> Bool
}
```
If you set both formatter and validator, format is removed before checking string validation.# Setup
To set `DGInputField` configuration use `TextFieldConfiguration` like in example:
```swift
let configuration = TextFieldConfiguration(
formatter: CardNumberFormatter(),
validator: CardNumberInputValidator())
creditCardField.setConfiguration(configuration)
```
Thats it!# Licensing
This repository's code is distributed under **MIT** license.