Skip to content

Commit 00fca04

Browse files
committed
Fix currencyTextField - correct cursor after paste
1 parent 1f7e59c commit 00fca04

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

Sources/SwiftUIKit/views/CurrencyTextField.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)