Simple text formatting in Swift
Text formatting framework written on Swift 5.0.
Features | |
---|---|
🎭 | Convert string into formatted string and vice versa |
🚴 | Formatting text during typing |
#️⃣ | Set format using ‘#’ characters like ‘### ##-###’ |
😛 | Supporting emojis |
💲 | Formatting money amount |
🅿️ | Formatting with placeholders |
🌱 | UITextField and UITextView support |
To run the example project, clone the repo and run pod install
from the Example directory first.
AnyFormatKit has SwiftUI version, that exists as separate framework AnyFormatKitSwiftUI
AnyFormatKit is available through CocoaPods. To install
it, simply add the following line to your Podfile:
pod 'AnyFormatKit', '~> 2.5.2'
Then, run the following command:
$ pod install
AnyFormatKit is available with Swift Package Manager.
Once you have your Swift package set up, than simply add AnyFormatKit to the dependencies
value of your Package.swift
dependencies: [
.package(url: "https://github.com/luximetr/AnyFormatKit.git", .upToNextMajor(from: "2.5.2"))
]
import AnyFormatKit
let phoneFormatter = DefaultTextInputFormatter(textPattern: "### (###) ###-##-##")
let phoneInputController = TextFieldInputController()
textField.delegate = phoneInputController
phoneInputController.formatter = phoneFormatter
phoneNumberFormatter.unformat("+51 (013) 442-55-11") // +51013442551
textField.delegate
by yourselflet phoneFormatter = DefaultTextInputFormatter(textPattern: "### (###) ###-##-##")
// inside of UITextFieldDelegate shouldChangeTextIn method
let result = formatter.formatInput(currentText: textField.text ?? "", range: range, replacementString: string)
textField.text = result.formattedText
textField.setCursorLocation(result.caretBeginOffset)
You can find example of
setCursorLocation
here
DefaultTextInputFormatter
- formatting symbol by symbolSumTextInputFormatter
- formatting like a money formatPlaceholderTextInputFormatter
- formatting with all textPattern as placeholderluximetr, [email protected]
AnyFormatKit is available under the MIT license. See the LICENSE file for more info.