From 4cf369d4aba51eb13eabd84a59e7f5546be5837a Mon Sep 17 00:00:00 2001 From: Steve Hannah Date: Sat, 22 Feb 2025 07:09:18 -0800 Subject: [PATCH] fix: [3851] fixed iOS losing cursor position when using predictive text Fixes https://github.com/codenameone/CodenameOne/issues/3851 --- Ports/iOSPort/nativeSources/IOSNative.m | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Ports/iOSPort/nativeSources/IOSNative.m b/Ports/iOSPort/nativeSources/IOSNative.m index 2e4d711ae7..2c06eb022a 100644 --- a/Ports/iOSPort/nativeSources/IOSNative.m +++ b/Ports/iOSPort/nativeSources/IOSNative.m @@ -7610,7 +7610,17 @@ void com_codename1_impl_ios_IOSNative_updateNativeEditorText___java_lang_String( NSString* nsText = toNSString(CN1_THREAD_GET_STATE_PASS_ARG text); NSString* currText = ((UITextView*)editingComponent).text; if (![nsText isEqualToString:currText]) { - ((UITextView*)editingComponent).text = nsText; + UITextView *textView = (UITextView *)editingComponent; + + // Save current cursor position + NSRange selectedRange = textView.selectedRange; + + // Update the text + textView.text = nsText; + + // Restore the cursor position + NSUInteger newPosition = MIN(selectedRange.location, textView.text.length); + textView.selectedRange = NSMakeRange(newPosition, 0); } } POOL_END();