Prevent caret from changing position on auto complete
This commit is contained in:
parent
ae272582ac
commit
58c6d508ec
|
@ -70,11 +70,17 @@ struct ComposeContentWarningTextField: UIViewRepresentable {
|
|||
return
|
||||
}
|
||||
|
||||
let distanceToEnd = textField.offset(from: selectedRange.start, to: textField.endOfDocument)
|
||||
|
||||
let selectedRangeStartUTF16 = textField.offset(from: textField.beginningOfDocument, to: selectedRange.start)
|
||||
let characterBeforeCursorIndex = text.utf16.index(text.startIndex, offsetBy: selectedRangeStartUTF16)
|
||||
|
||||
textField.text!.replaceSubrange(lastWordStartIndex..<characterBeforeCursorIndex, with: string)
|
||||
didChange(textField)
|
||||
|
||||
// keep the cursor at the same position in the text, immediately after what was inserted
|
||||
let newCursorPosition = textField.position(from: textField.endOfDocument, offset: -distanceToEnd)!
|
||||
textField.selectedTextRange = textField.textRange(from: newCursorPosition, to: newCursorPosition)
|
||||
}
|
||||
|
||||
private func updateAutocompleteState(textField: UITextField) {
|
||||
|
|
|
@ -239,10 +239,15 @@ struct MainComposeWrappedTextView: UIViewRepresentable {
|
|||
return
|
||||
}
|
||||
|
||||
let distanceToEnd = text.utf16.count - textView.selectedRange.upperBound
|
||||
|
||||
let characterBeforeCursorIndex = text.utf16.index(text.startIndex, offsetBy: textView.selectedRange.upperBound)
|
||||
|
||||
textView.text.replaceSubrange(lastWordStartIndex..<characterBeforeCursorIndex, with: string)
|
||||
self.textViewDidChange(textView)
|
||||
|
||||
// keep the cursor at the same position in the text, immediately after what was inserted
|
||||
textView.selectedRange = NSRange(location: textView.text.utf16.count - distanceToEnd, length: 0)
|
||||
}
|
||||
|
||||
private func updateAutocompleteState() {
|
||||
|
|
Loading…
Reference in New Issue