-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[iOS 26] - The MaxLength property value is not respected on an Entry control - fix #32045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: net10.0
Are you sure you want to change the base?
Conversation
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.
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. |
There was a problem hiding this 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.
Wouldn't it be better to use a runtime check instead of a compile-time check? |
/azp run |
Azure Pipelines successfully started running 3 pipeline(s). |
There was a problem hiding this 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.
return VirtualView?.TextWithinMaxLength(textField.Text, ranges[0].RangeValue, replacementString) ?? false; | ||
|
Copilot
AI
Oct 20, 2025
There was a problem hiding this comment.
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.
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) |
Copilot
AI
Oct 20, 2025
There was a problem hiding this comment.
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; | ||
|
||
return true; | ||
} | ||
#else | ||
bool OnShouldChangeCharacters(UITextField textField, NSRange range, string replacementString) => | ||
VirtualView?.TextWithinMaxLength(textField.Text, range, replacementString) ?? false; | ||
#endif | ||
|
Copilot
AI
Oct 20, 2025
There was a problem hiding this comment.
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.
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.
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 | ||
|
Copilot
AI
Oct 20, 2025
There was a problem hiding this comment.
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.
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.
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:)
https://sebvidal.com/blog/whats-new-in-uikit-26/

Issues Fixed
Fixes #32016