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 |
To run the example project, clone the repo and run pod install
from the Example directory first.
- iOS 8.0+
- Swift 4.0+
- Xcode 9.0+
AnyFormatKit is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'AnyFormatKit', '~> 2.2.0'
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.2.0"))
]
import AnyFormatKit
let phoneFormatter = DefaultTextFormatter(textPattern: "### (###) ###-##-##")
phoneFormatter.format("+123456789012") // +12 (345) 678-90-12
let customFormatter = DefaultTextFormatter(textPattern: "###-###custom###-###")
customFormatter.format("111222333444") // 111-222custom333-444
You can also set your own symbol in the pattern
let cardFormatter = DefaultTextFormatter(textPattern: "XXXX XXXX XXXX XXXX", patternSymbol: "X")
cardFormatter.format("4444555566667777") // 4444 5555 6666 7777
For string with different length
let formatter = DefaultTextFormatter(textPattern: "## ###-##")
formatter.format("1234") // 12 34
formatter.format("123456789") // 12 345-67
Unformatting
let formatter = DefaultTextFormatter(textPattern: "## ###-##")
formatter.unformat("99 888-77") // 9988877
let phoneFormatter = PlaceholderTextFormatter(textPattern: "### (###) ###-##-##")
phoneFormatter.format("+123") // +12 (3##) ###-##-##
let formatter = SumTextFormatter(textPattern: "#,###.##")
formatter.format("1234.13") // 1,234.13
Using DefaultTextInputFormatter
formatter
let formatter = DefaultTextInputFormatter(textPattern: "### (###) ###-##-##")
// inside of UITextFieldDelegate shouldChangeTextIn method
let result = formatter.formatInput(currentText: textView.text, range: range, replacementString: text)
textView.text = result.formattedText
textView.setCursorLocation(result.caretBeginOffset)
Using SumTextInputFormatter
formatter
let formatter = SumTextInputFormatter(textPattern: "#,###.##$")
// inside of UITextFieldDelegate shouldChangeTextIn method
let result = formatter.formatInput(currentText: textView.text, range: range, replacementString: text)
textView.text = result.formattedText
textView.setCursorLocation(result.caretBeginOffset)
Using PlaceholderTextInputFormatter
formatter
let formatter = PlaceholderTextInputFormatter(textPattern: "#### #### #### ####")
// inside of UITextFieldDelegate shouldChangeTextIn method
let result = formatter.formatInput(currentText: textView.text, range: range, replacementString: text)
textView.text = result.formattedText
textView.setCursorLocation(result.caretBeginOffset)
luximetr, [email protected]
AnyFormatKit is available under the MIT license. See the LICENSE file for more info.