8
8
9
9
import UIKit
10
10
11
+ @objc public protocol FormattedTextFieldDelegate : UITextFieldDelegate {
12
+ @objc optional func textField( _ textField: UITextField , shouldChangeUnformattedText text: String , in range: NSRange , replacementString: String ) -> Bool
13
+ }
14
+
11
15
open class FormattedTextField : UITextField {
12
16
open static let maskSymbol : Character = " × "
13
17
@@ -28,7 +32,11 @@ open class FormattedTextField: UITextField {
28
32
private func commonInit( ) {
29
33
delegateProxy. delegate = super. delegate
30
34
super. delegate = delegateProxy
31
- text = super. text
35
+
36
+ if let unformattedText = unformattedText {
37
+ var cursorPosition = 0
38
+ text = formattedText ( fromText: unformattedText, textMask: text, cursorPosition: & cursorPosition)
39
+ }
32
40
33
41
placeholderLabel. font = font
34
42
placeholderLabel. textColor = UIColor ( white: 170 / 255.0 , alpha: 0.5 )
@@ -42,28 +50,21 @@ open class FormattedTextField: UITextField {
42
50
43
51
@IBInspectable open var textMask : String ? {
44
52
didSet( oldMask) {
45
- var cursorPosition = 0
46
- let unformattedText = unformatterText ( fromText: ( formattedText ?? " " ) , textMask: oldMask, cursorPosition: & cursorPosition)
47
- formattedText = formattedText ( fromText: unformattedText, textMask: textMask, cursorPosition: & cursorPosition)
48
- }
49
- }
53
+ var cursorPosition : Int = 0
50
54
51
- open private( set) var formattedText : String ? {
52
- get {
53
- return super. text
54
- }
55
- set ( value) {
56
- super. text = value
55
+ let unformattedText = unformatterText ( fromText: ( text ?? " " ) , textMask: oldMask, cursorPosition: & cursorPosition)
56
+ let newFormattedText = formattedText ( fromText: unformattedText, textMask: textMask, cursorPosition: & cursorPosition)
57
+ text = newFormattedText
57
58
}
58
59
}
59
60
60
- open override var text : String ? {
61
+ @ IBInspectable open var unformattedText : String ? {
61
62
get {
62
- guard let formattedText = self . formattedText else {
63
+ guard let text = text else {
63
64
return nil
64
65
}
65
66
var cursorPosition = 0
66
- let unformattedText = self . unformatterText ( fromText: formattedText , textMask: textMask, cursorPosition: & cursorPosition)
67
+ let unformattedText = self . unformatterText ( fromText: text , textMask: textMask, cursorPosition: & cursorPosition)
67
68
68
69
return unformattedText
69
70
}
@@ -72,9 +73,9 @@ open class FormattedTextField: UITextField {
72
73
let unformattedText = value ?? " "
73
74
let formattedText = self . formattedText ( fromText: unformattedText, textMask: textMask, cursorPosition: & cursorPosition)
74
75
if formattedText. characters. count > 0 || value != nil {
75
- self . formattedText = formattedText
76
+ text = formattedText
76
77
} else {
77
- self . formattedText = nil
78
+ text = nil
78
79
}
79
80
placeholderLabel. isHidden = ( unformattedText. characters. count > 0 )
80
81
}
@@ -144,6 +145,7 @@ open class FormattedTextField: UITextField {
144
145
}
145
146
146
147
// MARK: - Private
148
+
147
149
private var maskSymbol : Character {
148
150
return type ( of: self ) . maskSymbol
149
151
}
@@ -157,7 +159,13 @@ open class FormattedTextField: UITextField {
157
159
} ( )
158
160
159
161
private func shouldChangeCharacters( in range: NSRange , replacementString string: String ) -> Bool {
160
- let formattedText = self . formattedText ?? " "
162
+ if let shouldChange = delegateProxy. delegate? . textField ? ( self , shouldChangeCharactersIn: range, replacementString: string) {
163
+ if !shouldChange {
164
+ return false
165
+ }
166
+ }
167
+
168
+ let formattedText = text ?? " "
161
169
var charachtersRange = formattedText. nsrange ( fromRange: formattedText. range ( fromUtf16NsRange: range) !)
162
170
163
171
var cursorPosition : Int
@@ -171,11 +179,11 @@ open class FormattedTextField: UITextField {
171
179
var unformattedText = unformatterText ( fromText: formattedText, textMask: textMask, cursorPosition: & cursorPosition)
172
180
let unformattedRange = self . range ( fromFormattedRange: charachtersRange)
173
181
174
- if let originDelegate = delegateProxy. delegate,
175
- originDelegate. responds ( to: #selector( UITextFieldDelegate . textField ( _: shouldChangeCharactersIn : replacementString: ) ) ) {
182
+ if let originDelegate = ( delegateProxy. delegate as? FormattedTextFieldDelegate ) ,
183
+ originDelegate. responds ( to: #selector( FormattedTextFieldDelegate . textField ( _: shouldChangeUnformattedText : in : replacementString: ) ) ) {
176
184
177
185
let utf16UnformattedRange = unformattedText. utf16Nsrange ( fromRange: unformattedText. range ( fromNsRange: unformattedRange) !)
178
- if !originDelegate. textField!( self , shouldChangeCharactersIn : utf16UnformattedRange, replacementString: string) {
186
+ if !originDelegate. textField!( self , shouldChangeUnformattedText : unformattedText , in : utf16UnformattedRange, replacementString: string) {
179
187
return false
180
188
}
181
189
}
@@ -184,15 +192,12 @@ open class FormattedTextField: UITextField {
184
192
cursorPosition += string. characters. count
185
193
186
194
let newFormattedText = self . formattedText ( fromText: unformattedText, textMask: textMask, cursorPosition: & cursorPosition)
187
- self . formattedText = newFormattedText
195
+ text = newFormattedText
188
196
placeholderLabel. isHidden = ( unformattedText. characters. count > 0 )
189
197
190
198
cursorPosition = min ( cursorPosition, newFormattedText. characters. count)
191
199
let cursorIndex = newFormattedText. index ( newFormattedText. startIndex, offsetBy: cursorPosition)
192
- let utf16CursorPosition = newFormattedText. utf16Nsrange ( fromRange: cursorIndex..< cursorIndex) . location
193
-
194
- let targetPosition = position ( from: beginningOfDocument, offset: utf16CursorPosition) !
195
- selectedTextRange = self . textRange ( from: targetPosition, to: targetPosition)
200
+ selectedCharachtersRange = cursorIndex..< cursorIndex
196
201
197
202
sendActions ( for: . editingChanged)
198
203
0 commit comments