Skip to content

Conversation

kubaflo
Copy link
Contributor

@kubaflo kubaflo commented Oct 17, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description of Change

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.

https://developer.apple.com/documentation/uikit/uitextfielddelegate/textfield(_:shouldchangecharactersin:replacementstring:)

Screenshot 2025-10-17 at 16 14 05

https://sebvidal.com/blog/whats-new-in-uikit-26/
Screenshot 2025-10-17 at 16 14 21

Issues Fixed

Fixes #32016

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.
@Copilot Copilot AI review requested due to automatic review settings October 17, 2025 14:13
@kubaflo kubaflo changed the base branch from main to net10.0 October 17, 2025 14:13
@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Oct 17, 2025
@dotnet-policy-service
Copy link
Contributor

Hey there @@kubaflo! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@kubaflo kubaflo changed the title Fix 32016 [iOS 26] - The MaxLength property value is not respected on an Entry control - fix Oct 17, 2025
@kubaflo kubaflo self-assigned this Oct 17, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@baaaaif
Copy link

baaaaif commented Oct 17, 2025

Wouldn't it be better to use a runtime check instead of a compile-time check?
Like done in this iOS 26 fix https://github.com/dotnet/maui/pull/31831/files

@jfversluis
Copy link
Member

/azp run

@jfversluis jfversluis requested a review from Copilot October 20, 2025 11:43
@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 4 comments.

Comment on lines +228 to +229
return VirtualView?.TextWithinMaxLength(textField.Text, ranges[0].RangeValue, replacementString) ?? false;

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.

}

#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.

Comment on lines +227 to 236
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

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
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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community ✨ Community Contribution platform/ios

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants