From 0cbc2024f4e8b2f14a421a1e6da6b4b6fe82f510 Mon Sep 17 00:00:00 2001 From: Jakub Florkowski Date: Fri, 17 Oct 2025 16:12:38 +0200 Subject: [PATCH 1/2] Add support for ShouldChangeCharactersInRanges on iOS 26+ Introduces handling for the ShouldChangeCharactersInRanges event in EntryHandler for iOS 26.0 or greater, ensuring text changes respect max length constraints for multiple ranges. Event subscription and unsubscription are conditionally compiled for newer iOS versions. --- src/Core/src/Handlers/Entry/EntryHandler.iOS.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Core/src/Handlers/Entry/EntryHandler.iOS.cs b/src/Core/src/Handlers/Entry/EntryHandler.iOS.cs index e6e2030716c8..e9c8a81ec5b1 100644 --- a/src/Core/src/Handlers/Entry/EntryHandler.iOS.cs +++ b/src/Core/src/Handlers/Entry/EntryHandler.iOS.cs @@ -137,6 +137,9 @@ public void Connect(IEntry virtualView, MauiTextField platformView) platformView.EditingDidEnd += OnEditingEnded; platformView.TextPropertySet += OnTextPropertySet; platformView.ShouldChangeCharacters += OnShouldChangeCharacters; +#if IOS26_0_OR_GREATER + platformView.ShouldChangeCharactersInRanges += ShouldChangeCharactersInRanges; +#endif } public void Disconnect(MauiTextField platformView) @@ -149,6 +152,9 @@ public void Disconnect(MauiTextField platformView) platformView.EditingDidEnd -= OnEditingEnded; platformView.TextPropertySet -= OnTextPropertySet; platformView.ShouldChangeCharacters -= OnShouldChangeCharacters; +#if IOS26_0_OR_GREATER + platformView.ShouldChangeCharactersInRanges -= ShouldChangeCharactersInRanges; +#endif if (_set) platformView.SelectionChanged -= OnSelectionChanged; @@ -213,6 +219,15 @@ void OnTextPropertySet(object? sender, EventArgs e) } } +#if IOS26_0_OR_GREATER + bool ShouldChangeCharactersInRanges(UITextField textField, NSValue[] ranges, string replacementString) + { + if (ranges is { Length: > 0 }) + return VirtualView?.TextWithinMaxLength(textField.Text, ranges[0].RangeValue, replacementString) ?? true; + + return true; + } +#endif bool OnShouldChangeCharacters(UITextField textField, NSRange range, string replacementString) => VirtualView?.TextWithinMaxLength(textField.Text, range, replacementString) ?? false; From 6157e44cf97921569e7473afbc852e18dd044420 Mon Sep 17 00:00:00 2001 From: Jakub Florkowski Date: Fri, 17 Oct 2025 19:08:59 +0200 Subject: [PATCH 2/2] Update EntryHandler.iOS.cs --- src/Core/src/Handlers/Entry/EntryHandler.iOS.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Core/src/Handlers/Entry/EntryHandler.iOS.cs b/src/Core/src/Handlers/Entry/EntryHandler.iOS.cs index e9c8a81ec5b1..ac5269e00ddf 100644 --- a/src/Core/src/Handlers/Entry/EntryHandler.iOS.cs +++ b/src/Core/src/Handlers/Entry/EntryHandler.iOS.cs @@ -136,9 +136,10 @@ public void Connect(IEntry virtualView, MauiTextField platformView) platformView.EditingChanged += OnEditingChanged; platformView.EditingDidEnd += OnEditingEnded; platformView.TextPropertySet += OnTextPropertySet; - platformView.ShouldChangeCharacters += OnShouldChangeCharacters; #if IOS26_0_OR_GREATER platformView.ShouldChangeCharactersInRanges += ShouldChangeCharactersInRanges; +#else + platformView.ShouldChangeCharacters += OnShouldChangeCharacters; #endif } @@ -151,9 +152,10 @@ public void Disconnect(MauiTextField platformView) platformView.EditingChanged -= OnEditingChanged; platformView.EditingDidEnd -= OnEditingEnded; platformView.TextPropertySet -= OnTextPropertySet; - platformView.ShouldChangeCharacters -= OnShouldChangeCharacters; #if IOS26_0_OR_GREATER platformView.ShouldChangeCharactersInRanges -= ShouldChangeCharactersInRanges; +#else + platformView.ShouldChangeCharacters -= OnShouldChangeCharacters; #endif if (_set) @@ -223,13 +225,14 @@ void OnTextPropertySet(object? sender, EventArgs e) bool ShouldChangeCharactersInRanges(UITextField textField, NSValue[] ranges, string replacementString) { if (ranges is { Length: > 0 }) - return VirtualView?.TextWithinMaxLength(textField.Text, ranges[0].RangeValue, replacementString) ?? true; + return VirtualView?.TextWithinMaxLength(textField.Text, ranges[0].RangeValue, replacementString) ?? false; return true; } -#endif +#else bool OnShouldChangeCharacters(UITextField textField, NSRange range, string replacementString) => VirtualView?.TextWithinMaxLength(textField.Text, range, replacementString) ?? false; +#endif void OnSelectionChanged(object? sender, EventArgs e) {