You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import Foundation
import UIKit
classDefaultTextFormatter:NSObject{publiclettextPattern:String/// Symbol that will be replace by input symbolspublicletpatternSymbol:Characterpublicinit(textPattern:String,
patternSymbol:Character="#"){self.textPattern = textPattern
self.patternSymbol = patternSymbol
}func format(_ unformattedText:String?)->String?{
guard let unformattedText = unformattedText else{returnnil}varformatted=String.init()varunformattedIndex=0varpatternIndex=0
while patternIndex < textPattern.count && unformattedIndex < unformattedText.count {
guard let patternCharacter = textPattern.characterAt(patternIndex)else{ break }
if patternCharacter == patternSymbol {
if let unformattedCharacter = unformattedText.characterAt(unformattedIndex){
formatted.append(unformattedCharacter)}
unformattedIndex +=1}else{
formatted.append(patternCharacter)}
patternIndex +=1}return formatted
}func unformat(_ formatted:String?)->String?{
guard let formatted = formatted else{returnnil}
if textPattern.starts(with: formatted){return formatted
}varunformatted=String()varformattedIndex=0
while formattedIndex < formatted.count {
if let formattedCharacter = formatted.characterAt(formattedIndex){
if formattedIndex >= textPattern.count {
unformatted.append(formattedCharacter)}else if formattedCharacter != textPattern.characterAt(formattedIndex){
unformatted.append(formattedCharacter)}
formattedIndex +=1}}return unformatted
}}extensionDefaultTextFormatter:UITextFieldDelegate{func textField(
_ textField:UITextField,
shouldChangeCharactersIn range:NSRange,
replacementString string:String)->Bool{letnewText=(textField.text asNSString?)?.replacingCharacters(in: range, with: string)letunformattedText=unformat(newText)// Обход проблемы, когда юзер начинает ввод с символа, с которого начинается паттерн// (не возможно было начать ввод с 8)
if textPattern.starts(with: newText ??""){
textField.text = newText
}else{
textField.text =format(unformattedText)?.uppercased()}
textField.sendActions(for:UIControl.Event.editingChanged)return false
}}
The text was updated successfully, but these errors were encountered:
Now we implement textformatting support inside project, but we can implement support of formatting out of box.
for example with integration of https://github.com/redmadrobot/input-mask-android and https://github.com/redmadrobot/input-mask-ios as we do inside our projects.
or with https://github.com/artemkrachulov/AKMaskField on iOS
or by custom code:
The text was updated successfully, but these errors were encountered: