|
| 1 | +import UIKit |
| 2 | +import Foundation |
| 3 | + |
| 4 | +/// Adds formatting (decoration) characters to text field's content according to a variable pattern. Can be used for |
| 5 | +/// payment card number formatting, phone number formatting, etc. |
| 6 | +public final class DecoratingTextFieldDelegate: NSObject { |
| 7 | + public let patternForDataString: (String) -> String |
| 8 | + public let patternPlaceholderForDataCharacter: Character |
| 9 | + public let isDataCharacter: (Character) -> Bool |
| 10 | + |
| 11 | + /** |
| 12 | + Intializes a delegate with a fixed pattern |
| 13 | + - parameters: |
| 14 | + - pattern: a string containing data placeholder and formatting characters. |
| 15 | + - patternPlaceholderForDataCharacter: a character that is not a formatting character. |
| 16 | + - isDataCharacter: a predicate to filter non-data characters from user's input. No matter what user tries to put \ |
| 17 | + into the textfield, only characters for which `isDataCharacter` returns `true` will appear in the text field. |
| 18 | + ## Example: |
| 19 | + A 16-digit VISA payment card pattern might look like this `####-####-####-####` `'#'` is a |
| 20 | + `patternPlaceholderForDataCharacter` and '`-`' is a formatting (decorating) character. |
| 21 | + */ |
| 22 | + public convenience init( |
| 23 | + pattern: String, |
| 24 | + patternPlaceholderForDataCharacter: Character, |
| 25 | + isDataCharacter: @escaping (Character) -> Bool) |
| 26 | + { |
| 27 | + self.init( |
| 28 | + patternForDataString: { _ in pattern }, |
| 29 | + patternPlaceholderForDataCharacter: patternPlaceholderForDataCharacter, |
| 30 | + isDataCharacter: isDataCharacter |
| 31 | + ) |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + Intializes a delegate with a fixed pattern |
| 36 | + - parameters: |
| 37 | + - patternForDataString: `DecoratingTextFieldDelegate` will call this function passing current data string as a \ |
| 38 | + parameter every time the data string changes, the returned pattern will subsequently be used to format the data \ |
| 39 | + string passed. |
| 40 | + - patternPlaceholderForDataCharacter: a character that is not a formatting character. |
| 41 | + - isDataCharacter: a predicate to filter non-data characters from user's input. No matter what user tries to put \ |
| 42 | + into the textfield, only characters for which `isDataCharacter` returns `true` will appear in the text field. |
| 43 | + ## Example: |
| 44 | + A 16-digit VISA payment card pattern might look like this `####-####-####-####` `'#'` is a |
| 45 | + `patternPlaceholderForDataCharacter` and '`-`' is a formatting (decorating) character. Furthermore, to support \ |
| 46 | + various kinds of payment cards a more complex behaviour may need to be implemented where the first 6 digits of a \ |
| 47 | + payment card number will define total length and formatting pattern for any valid card number starting with those 6 \ |
| 48 | + digits. This behaviour can be implemented by using `patternForDataString`. |
| 49 | + */ |
| 50 | + public init( |
| 51 | + patternForDataString: @escaping (String) -> String, |
| 52 | + patternPlaceholderForDataCharacter: Character, |
| 53 | + isDataCharacter: @escaping (Character) -> Bool) |
| 54 | + { |
| 55 | + self.patternForDataString = patternForDataString |
| 56 | + self.patternPlaceholderForDataCharacter = patternPlaceholderForDataCharacter |
| 57 | + self.isDataCharacter = isDataCharacter |
| 58 | + super.init() |
| 59 | + } |
| 60 | + |
| 61 | + /// - Parameters: |
| 62 | + /// - dataString: a string conaining only data characters (see `isDataCharacter`). |
| 63 | + /// - Returns: a representation of `dataString` formatted using a corresponding pattern obtained using \ |
| 64 | + /// `patternForDataString`. |
| 65 | + public func decorateString(_ dataString: String) -> String { |
| 66 | + var res = "" |
| 67 | + var dataIndex = dataString.startIndex |
| 68 | + let pattern = self.patternForDataString(dataString) |
| 69 | + for patternChar in pattern { |
| 70 | + if patternChar == patternPlaceholderForDataCharacter { |
| 71 | + if dataIndex == dataString.endIndex { |
| 72 | + return res |
| 73 | + } |
| 74 | + res += String(dataString[dataIndex]) |
| 75 | + dataIndex = dataString.index(after: dataIndex) |
| 76 | + } else { |
| 77 | + res += String(patternChar) |
| 78 | + } |
| 79 | + } |
| 80 | + return res |
| 81 | + } |
| 82 | + |
| 83 | + /// Strips formatting (decoration) characters from the input string. |
| 84 | + public func undecorateString(_ decoratedString: String) -> String { |
| 85 | + var res = "" |
| 86 | + for decoChar in decoratedString { |
| 87 | + if isDataCharacter(decoChar) { |
| 88 | + res += String(decoChar) |
| 89 | + } |
| 90 | + } |
| 91 | + return res |
| 92 | + } |
| 93 | + |
| 94 | + fileprivate func convertDecoRange(_ decoRange: NSRange, fromDecoratedString decoratedString: String) -> NSRange { |
| 95 | + let decoPrefix = (decoratedString as NSString).substring(to: decoRange.location) |
| 96 | + let decoSubstring = (decoratedString as NSString).substring(with: decoRange) |
| 97 | + let dataPrefix = self.undecorateString(decoPrefix) |
| 98 | + let dataSubstring = self.undecorateString(decoSubstring) |
| 99 | + return NSRange(location: dataPrefix.nsLength, length: dataSubstring.nsLength) |
| 100 | + } |
| 101 | + |
| 102 | + fileprivate func convertDataLocation(_ dataLocation: Int, toDecoratedString decoratedString: String) -> Int { |
| 103 | + if dataLocation <= 0 { |
| 104 | + return dataLocation |
| 105 | + } |
| 106 | + var res = 0 |
| 107 | + var prefixLength = dataLocation |
| 108 | + for decoChar in decoratedString { |
| 109 | + let characterLength = String(decoChar).nsLength |
| 110 | + if isDataCharacter(decoChar) { |
| 111 | + if prefixLength <= 0 { |
| 112 | + return res |
| 113 | + } |
| 114 | + prefixLength -= characterLength |
| 115 | + } |
| 116 | + res += characterLength |
| 117 | + } |
| 118 | + return decoratedString.nsLength |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +extension DecoratingTextFieldDelegate: UITextFieldDelegate { |
| 123 | + public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { |
| 124 | + let decoString = textField.text ?? "" |
| 125 | + let decoReplacement = string |
| 126 | + let dataString = undecorateString(decoString) |
| 127 | + let dataReplacement = undecorateString(decoReplacement) |
| 128 | + var dataRange = convertDecoRange(range, fromDecoratedString: decoString) |
| 129 | + if range.length > 0 && decoReplacement.isEmpty && dataRange.length == 0 && dataRange.location > 0 { |
| 130 | + // probably backspace was hit with no data characters selected or prior to cursor |
| 131 | + // in this case we grow data range by one prior data character (if possible) |
| 132 | + // in order to erase that data character |
| 133 | + dataRange = (dataString as NSString).rangeOfComposedCharacterSequence(at: dataRange.location - 1) |
| 134 | + } |
| 135 | + |
| 136 | + let newDataString = (dataString as NSString).replacingCharacters(in: dataRange, with: dataReplacement) |
| 137 | + let newDecoString = decorateString(newDataString) |
| 138 | + textField.text = newDecoString |
| 139 | + textField.sendActions(for: .editingChanged) |
| 140 | + |
| 141 | + let newDataLocation = dataRange.location + dataReplacement.nsLength |
| 142 | + let newDecoLocation = convertDataLocation(newDataLocation, toDecoratedString: newDecoString) |
| 143 | + if let selPos = textField.position(from: textField.beginningOfDocument, offset: newDecoLocation) { |
| 144 | + textField.selectedTextRange = textField.textRange(from: selPos, to: selPos) |
| 145 | + } |
| 146 | + return false |
| 147 | + } |
| 148 | +} |
0 commit comments