From 756d25dde1695530189a97389967856ec8f5270d Mon Sep 17 00:00:00 2001 From: Doug Date: Mon, 5 Aug 2024 12:26:39 +0100 Subject: [PATCH] Fix a bug where moving a new line in the composer could move the caret. --- .../ComposerToolbar/View/MessageComposerTextField.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ElementX/Sources/Screens/RoomScreen/ComposerToolbar/View/MessageComposerTextField.swift b/ElementX/Sources/Screens/RoomScreen/ComposerToolbar/View/MessageComposerTextField.swift index d2bc028132..1bbf06235c 100644 --- a/ElementX/Sources/Screens/RoomScreen/ComposerToolbar/View/MessageComposerTextField.swift +++ b/ElementX/Sources/Screens/RoomScreen/ComposerToolbar/View/MessageComposerTextField.swift @@ -105,6 +105,9 @@ private struct UITextViewWrapper: UIViewRepresentable { .foregroundColor: UIColor(.compound.textPrimary)] if textView.attributedText != text { + // Remember the selection if only the attributes have changed. + let selection = textView.attributedText.string == text.string ? textView.selectedTextRange : nil + textView.attributedText = text // Re-apply the default font when setting text for e.g. edits. @@ -120,6 +123,11 @@ private struct UITextViewWrapper: UIViewRepresentable { textView.keyboardType = .default textView.reloadInputViews() } + } else if let selection { + // Fixes a bug where pressing Return in the middle of two paragraphs + // moves the caret back to the bottom of the composer. + // https://github.com/element-hq/element-x-ios/issues/3104 + textView.selectedTextRange = selection } } }