-
Notifications
You must be signed in to change notification settings - Fork 29.1k
Fix[ios]: Correct capitalization when replacing selected text #174234
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: master
Are you sure you want to change the base?
Conversation
It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging. If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group. |
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.
Code Review
This pull request addresses an incorrect capitalization issue on iOS when replacing selected text. The fix introduces a property to store the original capitalization setting and dynamically adjusts autocapitalizationType
based on the cursor's position, ensuring the keyboard behaves as expected. The changes are logical and effectively solve the problem. I have included one suggestion to improve the conciseness of the new logic.
engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm
Outdated
Show resolved
Hide resolved
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.
overall makes sense. we will need to add some tests
@@ -150,6 +150,7 @@ FLUTTER_DARWIN_EXPORT | |||
|
|||
// UITextInputTraits | |||
@property(nonatomic) UITextAutocapitalizationType autocapitalizationType; | |||
@property(nonatomic) UITextAutocapitalizationType originalAutocapitalizationType; |
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.
can add a comment explaining why adding this? (why not just use autocapitalizationType
?)
BOOL shouldCapitalize = NO; | ||
if (selection.location == 0) { | ||
shouldCapitalize = YES; | ||
} else if (selection.location <= self.text.length) { |
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.
can location be equal to the length? (im asking cuz i don't know. @LongCatIsLooong probably knows)
@@ -1045,6 +1045,7 @@ - (void)configureWithDictionary:(NSDictionary*)configuration { | |||
_isSystemKeyboardEnabled = ShouldShowSystemKeyboard(inputType); | |||
self.keyboardType = ToUIKeyboardType(inputType); | |||
self.returnKeyType = ToUIReturnKeyType(configuration[kInputAction]); | |||
_originalAutocapitalizationType = ToUITextAutoCapitalizationType(configuration); |
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.
nit: self.originalAutocapitalizationType = ...
This commit resolves an issue on iOS where TextCapitalization.words would incorrectly capitalize new text when it replaced a selected portion of an existing word.
Problem:
When using a TextFormField with textCapitalization: TextCapitalization.words, the following steps would lead to incorrect behavior:
Root Cause:
Solution:
This fix addresses the issue by taking manual control of the keyboard's capitalization state. The logic is now placed in setSelectedTextRangeLocal:, which acts as a reliable chokepoint for all selection changes, including typing, programmatic changes, and user-driven dragging of selection handles.
On each change, the code determines if the selection is at a word boundary, dynamically toggles the view's autocapitalizationType between Words and None, and then calls [self reloadInputViews] to reload the iOS keyboard to immediately discard its old state and reload its configuration based on the property we just changed.
This ensures that the keyboard's capitalization state is always in sync with the user's selection, correctly mimicking native behavior and resolving the bug.
Screen.Recording.2025-08-21.at.12.58.04.PM.mov
Fixes #127922
Pre-launch Checklist
///
).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assist
bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.