File tree Expand file tree Collapse file tree 1 file changed +11
-1
lines changed Expand file tree Collapse file tree 1 file changed +11
-1
lines changed Original file line number Diff line number Diff line change @@ -253,7 +253,17 @@ public struct CurrencyTextField: UIViewRepresentable {
253253 let cursorLocation = textField. position ( from: beginningPosition, offset: offset)
254254
255255 if let cursorLoc = cursorLocation {
256- textField. selectedTextRange = textField. textRange ( from: cursorLoc, to: cursorLoc)
256+ /**
257+ Shortly after new text is being pasted from the clipboard, UITextField receives a new value for its
258+ `selectedTextRange` property from the system. This new range is not consistent to the formatted text and
259+ calculated caret position most of the time, yet it's being assigned just after setCaretPosition call.
260+
261+ To insure correct caret position is set, `selectedTextRange` is assigned asynchronously.
262+ (presumably after a vanishingly small delay)
263+ */
264+ DispatchQueue . main. asyncAfter ( deadline: DispatchTime . now ( ) ) {
265+ textField. selectedTextRange = textField. textRange ( from: cursorLoc, to: cursorLoc)
266+ }
257267 }
258268
259269 // prevent from going to didChange
You can’t perform that action at this time.
0 commit comments