https://github.com/orucanil/stringformatter
Simple Text Formetter (Credit Card Number, Phone Number, Serial Number etc.) Can be used in all text inputs according to the format pattern. If desired, large minor character restrictions can be made in the format pattern.
https://github.com/orucanil/stringformatter
character character-restrictions credit-card creditcard-validator formatter ios phone-format phone-number stringformatter stringformatter-extension swift swift3 swift4 swift5 text-formetter textfield textview validator
Last synced: 6 months ago
JSON representation
Simple Text Formetter (Credit Card Number, Phone Number, Serial Number etc.) Can be used in all text inputs according to the format pattern. If desired, large minor character restrictions can be made in the format pattern.
- Host: GitHub
- URL: https://github.com/orucanil/stringformatter
- Owner: orucanil
- License: mit
- Created: 2017-07-21T11:09:13.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-01-11T14:34:22.000Z (over 3 years ago)
- Last Synced: 2025-04-13T04:14:51.686Z (6 months ago)
- Topics: character, character-restrictions, credit-card, creditcard-validator, formatter, ios, phone-format, phone-number, stringformatter, stringformatter-extension, swift, swift3, swift4, swift5, text-formetter, textfield, textview, validator
- Language: Swift
- Homepage: https://www.linkedin.com/in/annul
- Size: 28.3 KB
- Stars: 252
- Watchers: 5
- Forks: 21
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# StringFormatter
Simple Text Formatter (Credit Card Number, Phone Number, Serial Number etc.) Can be used in all text inputs according to the format pattern. If desired, large minor character restrictions can be made in the format pattern.
TextFieldFormatter : https://github.com/orucanil/TextFieldFormatter
## Display Visual Example
----
Installation
--------------To use the StringFormatter extension in an app, just drag the StringFormatter extension file (demo files and assets are not needed) into your project.
Methods
--------------The StringFormatter extension has the following methods (note: for iOS, String in method arguments):
* func format(_ format: String, oldString: String) -> String
Formatting method according to given format pattern. oldString can be empty(""), but the final character formatting may not work smoothly.
* func unformat(_ format: String, oldString: String) -> String
Unformatting method according to given format pattern. oldString can be empty(""), but the final character unformatting may not work smoothly.
How to use ?
----------If the text format is entered uppercase, the character input from the keyboard is displayed as a upper case character.
'x' or 'X' -> Any character
'c' or 'C' -> Alphabetic character
'n' or 'N' -> Numerical character
```Swift
extension ViewController: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {guard let text = textField.text else {
return true
}
let lastText = (text as NSString).replacingCharacters(in: range, with: string) as Stringif textfieldPhoneNumber == textField {
textField.text = lastText.format("(NNN) NNN NN NN", oldString: text)
return false
} else if textfieldCreditCard == textField {
textField.text = lastText.format("nnnn nnnn nnnn nnnn", oldString: text)
return false
} else if textfieldSerialNumber == textField {
textField.text = lastText.format("XX NNNN", oldString: text)
return false
}
return true
}
}```
Build and run the project files. Enjoy more examples!