Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Core/src/Handlers/Entry/EntryHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ public void Connect(IEntry virtualView, MauiTextField platformView)
platformView.EditingChanged += OnEditingChanged;
platformView.EditingDidEnd += OnEditingEnded;
platformView.TextPropertySet += OnTextPropertySet;
#if IOS26_0_OR_GREATER
platformView.ShouldChangeCharactersInRanges += ShouldChangeCharactersInRanges;
#else
platformView.ShouldChangeCharacters += OnShouldChangeCharacters;
#endif
}

public void Disconnect(MauiTextField platformView)
Expand All @@ -148,7 +152,11 @@ public void Disconnect(MauiTextField platformView)
platformView.EditingChanged -= OnEditingChanged;
platformView.EditingDidEnd -= OnEditingEnded;
platformView.TextPropertySet -= OnTextPropertySet;
#if IOS26_0_OR_GREATER
platformView.ShouldChangeCharactersInRanges -= ShouldChangeCharactersInRanges;
#else
platformView.ShouldChangeCharacters -= OnShouldChangeCharacters;
#endif

if (_set)
platformView.SelectionChanged -= OnSelectionChanged;
Expand Down Expand Up @@ -213,8 +221,18 @@ void OnTextPropertySet(object? sender, EventArgs e)
}
}

#if IOS26_0_OR_GREATER
bool ShouldChangeCharactersInRanges(UITextField textField, NSValue[] ranges, string replacementString)
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New multi-range input handling lacks associated UI and shared tests to validate MaxLength enforcement when multiple selections/ranges are modified (e.g., batch deletes or multi-cursor replacements). Please add a HostApp issue page exercising multiple-range edits and a corresponding TestCases.Shared.Tests UITest to ensure behavior matches expectations.

Copilot uses AI. Check for mistakes.
{
if (ranges is { Length: > 0 })
return VirtualView?.TextWithinMaxLength(textField.Text, ranges[0].RangeValue, replacementString) ?? false;

Comment on lines +228 to +229
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only the first range (ranges[0]) is evaluated; additional ranges are ignored, which can allow combined edits to exceed MaxLength or incorrectly deny valid input. Iterate all ranges to compute the prospective final text before enforcing MaxLength (e.g., build the resulting string by applying each range mutation) or extend VirtualView.TextWithinMaxLength to accept multiple ranges.

Suggested change
return VirtualView?.TextWithinMaxLength(textField.Text, ranges[0].RangeValue, replacementString) ?? false;
{
// Apply all ranges to the text to simulate the final result
string originalText = textField.Text ?? string.Empty;
var textChars = originalText.ToCharArray();
var resultText = originalText;
// If multiple ranges, apply them in order
// iOS passes the replacement string for all ranges, so we apply the replacement string to each range
// To avoid index shifting, process ranges in reverse order
var sortedRanges = ranges.Select(r => r.RangeValue).OrderByDescending(r => r.Location).ToArray();
foreach (var range in sortedRanges)
{
resultText = resultText.Remove((int)range.Location, (int)range.Length)
.Insert((int)range.Location, replacementString);
}
// Now check if the result is within max length
return VirtualView?.TextWithinMaxLength(resultText, new Foundation.NSRange(0, resultText.Length), string.Empty) ?? false;
}

Copilot uses AI. Check for mistakes.
return true;
}
#else
bool OnShouldChangeCharacters(UITextField textField, NSRange range, string replacementString) =>
VirtualView?.TextWithinMaxLength(textField.Text, range, replacementString) ?? false;
#endif

Comment on lines +227 to 236
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Two separate methods duplicate MaxLength enforcement logic with different parameter shapes. Consider extracting a shared helper that computes the prospective text (supporting single or multiple ranges) to reduce duplication and ensure consistent behavior across iOS versions.

Suggested change
if (ranges is { Length: > 0 })
return VirtualView?.TextWithinMaxLength(textField.Text, ranges[0].RangeValue, replacementString) ?? false;
return true;
}
#else
bool OnShouldChangeCharacters(UITextField textField, NSRange range, string replacementString) =>
VirtualView?.TextWithinMaxLength(textField.Text, range, replacementString) ?? false;
#endif
return IsTextWithinMaxLength(textField.Text, replacementString, ranges);
}
#else
bool OnShouldChangeCharacters(UITextField textField, NSRange range, string replacementString) =>
IsTextWithinMaxLength(textField.Text, replacementString, range);
#endif
/// <summary>
/// Helper to compute the prospective text after replacement and check MaxLength.
/// Supports both single and multiple ranges.
/// </summary>
bool IsTextWithinMaxLength(string currentText, string replacementString, params object[] ranges)
{
if (VirtualView == null)
return false;
string prospectiveText = currentText;
if (ranges.Length == 1 && ranges[0] is NSRange singleRange)
{
prospectiveText = ReplaceTextInRange(currentText, singleRange, replacementString);
}
else if (ranges.Length > 0 && ranges[0] is NSValue)
{
// Multiple ranges (iOS 16+)
foreach (var obj in ranges)
{
if (obj is NSValue val)
{
var range = val.RangeValue;
prospectiveText = ReplaceTextInRange(prospectiveText, range, replacementString);
}
}
}
return VirtualView.MaxLength <= 0 || prospectiveText.Length <= VirtualView.MaxLength;
}
/// <summary>
/// Helper to replace text in a given NSRange.
/// </summary>
string ReplaceTextInRange(string text, NSRange range, string replacement)
{
if (range.Location > text.Length)
return text;
int start = (int)range.Location;
int length = (int)range.Length;
if (start + length > text.Length)
length = text.Length - start;
return text.Substring(0, start) + replacement + text.Substring(start + length);
}

Copilot uses AI. Check for mistakes.
Comment on lines +227 to 236
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Two separate methods duplicate MaxLength enforcement logic with different parameter shapes. Consider extracting a shared helper that computes the prospective text (supporting single or multiple ranges) to reduce duplication and ensure consistent behavior across iOS versions.

Suggested change
if (ranges is { Length: > 0 })
return VirtualView?.TextWithinMaxLength(textField.Text, ranges[0].RangeValue, replacementString) ?? false;
return true;
}
#else
bool OnShouldChangeCharacters(UITextField textField, NSRange range, string replacementString) =>
VirtualView?.TextWithinMaxLength(textField.Text, range, replacementString) ?? false;
#endif
var prospectiveText = ComputeProspectiveText(textField.Text, ranges, replacementString);
return VirtualView?.TextWithinMaxLength(prospectiveText) ?? false;
}
#else
bool OnShouldChangeCharacters(UITextField textField, NSRange range, string replacementString)
{
var prospectiveText = ComputeProspectiveText(textField.Text, range, replacementString);
return VirtualView?.TextWithinMaxLength(prospectiveText) ?? false;
}
#endif
/// <summary>
/// Computes the prospective text after applying the replacement string to the given ranges.
/// </summary>
string ComputeProspectiveText(string currentText, NSValue[] ranges, string replacementString)
{
if (ranges == null || ranges.Length == 0)
return currentText;
// For iOS 26+, multiple ranges can be replaced at once.
var text = currentText;
foreach (var rangeValue in ranges)
{
var range = rangeValue.RangeValue;
text = text.Remove((int)range.Location, (int)range.Length)
.Insert((int)range.Location, replacementString);
}
return text;
}
/// <summary>
/// Computes the prospective text after applying the replacement string to the given range.
/// </summary>
string ComputeProspectiveText(string currentText, NSRange range, string replacementString)
{
if (range.Length == 0)
return currentText.Insert((int)range.Location, replacementString);
return currentText.Remove((int)range.Location, (int)range.Length)
.Insert((int)range.Location, replacementString);
}

Copilot uses AI. Check for mistakes.
void OnSelectionChanged(object? sender, EventArgs e)
{
Expand Down
Loading