Skip to content

Commit ef8e09a

Browse files
committed
Remove unsafe overriding text property
1 parent d6535a4 commit ef8e09a

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

FormattedTextField/FormattedTextField.swift

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
import UIKit
1010

11+
@objc public protocol FormattedTextFieldDelegate: UITextFieldDelegate {
12+
@objc optional func textField(_ textField: UITextField, shouldChangeUnformattedText text: String, in range: NSRange, replacementString: String) -> Bool
13+
}
14+
1115
open class FormattedTextField: UITextField {
1216
open static let maskSymbol: Character = "×"
1317

@@ -28,7 +32,11 @@ open class FormattedTextField: UITextField {
2832
private func commonInit() {
2933
delegateProxy.delegate = super.delegate
3034
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+
}
3240

3341
placeholderLabel.font = font
3442
placeholderLabel.textColor = UIColor(white: 170/255.0, alpha: 0.5)
@@ -42,28 +50,21 @@ open class FormattedTextField: UITextField {
4250

4351
@IBInspectable open var textMask: String? {
4452
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
5054

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
5758
}
5859
}
5960

60-
open override var text: String? {
61+
@IBInspectable open var unformattedText: String? {
6162
get {
62-
guard let formattedText = self.formattedText else {
63+
guard let text = text else {
6364
return nil
6465
}
6566
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)
6768

6869
return unformattedText
6970
}
@@ -72,9 +73,9 @@ open class FormattedTextField: UITextField {
7273
let unformattedText = value ?? ""
7374
let formattedText = self.formattedText(fromText: unformattedText, textMask: textMask, cursorPosition: &cursorPosition)
7475
if formattedText.characters.count > 0 || value != nil {
75-
self.formattedText = formattedText
76+
text = formattedText
7677
} else {
77-
self.formattedText = nil
78+
text = nil
7879
}
7980
placeholderLabel.isHidden = (unformattedText.characters.count > 0)
8081
}
@@ -144,6 +145,7 @@ open class FormattedTextField: UITextField {
144145
}
145146

146147
// MARK: - Private
148+
147149
private var maskSymbol: Character {
148150
return type(of: self).maskSymbol
149151
}
@@ -157,7 +159,13 @@ open class FormattedTextField: UITextField {
157159
}()
158160

159161
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 ?? ""
161169
var charachtersRange = formattedText.nsrange(fromRange: formattedText.range(fromUtf16NsRange: range)!)
162170

163171
var cursorPosition: Int
@@ -171,11 +179,11 @@ open class FormattedTextField: UITextField {
171179
var unformattedText = unformatterText(fromText: formattedText, textMask: textMask, cursorPosition: &cursorPosition)
172180
let unformattedRange = self.range(fromFormattedRange: charachtersRange)
173181

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:))) {
176184

177185
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) {
179187
return false
180188
}
181189
}
@@ -184,15 +192,12 @@ open class FormattedTextField: UITextField {
184192
cursorPosition += string.characters.count
185193

186194
let newFormattedText = self.formattedText(fromText: unformattedText, textMask: textMask, cursorPosition: &cursorPosition)
187-
self.formattedText = newFormattedText
195+
text = newFormattedText
188196
placeholderLabel.isHidden = (unformattedText.characters.count > 0)
189197

190198
cursorPosition = min(cursorPosition, newFormattedText.characters.count)
191199
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
196201

197202
sendActions(for: .editingChanged)
198203

0 commit comments

Comments
 (0)