提交 82815836 编写于 作者: C Chris Bracken 提交者: GitHub

Smarter text/selection will/didChange events (#3699)

Check incoming text editing state and only fire textWillChange:,
textDidChange:, selectionWillChange:, selectionDidChange: when the text
or selection actually changes.

On selectionWillChange: in a text field where auto-correct is enabled,
iOS will attempt to auto-correct the word preceding the cursor. This
change also updates the text before calling selectionWillChange: to
prevent auto-correction on the preceding value of the text field.
上级 e3dd318f
......@@ -162,25 +162,32 @@ static UIKeyboardType ToUIKeyboardType(NSString* inputType) {
}
- (void)setTextInputState:(NSDictionary*)state {
[self.inputDelegate selectionWillChange:self];
[self.inputDelegate textWillChange:self];
[self.text setString:state[@"text"]];
NSString* newText = state[@"text"];
BOOL textChanged = ![self.text isEqualToString:newText];
if (textChanged) {
[self.inputDelegate textWillChange:self];
[self.text setString:newText];
}
NSInteger selectionBase = [state[@"selectionBase"] intValue];
NSInteger selectionExtent = [state[@"selectionExtent"] intValue];
NSUInteger start = MIN(MAX(0, MIN(selectionBase, selectionExtent)), (NSInteger)self.text.length);
NSUInteger end = MIN(MAX(0, MAX(selectionBase, selectionExtent)), (NSInteger)self.text.length);
NSRange selectedRange = NSMakeRange(start, end - start);
[self setSelectedTextRange:[FlutterTextRange rangeWithNSRange:selectedRange]
updateEditingState:NO];
_selectionAffinity = _kTextAffinityDownstream;
if ([state[@"selectionAffinity"] isEqualToString:@(_kTextAffinityUpstream)])
_selectionAffinity = _kTextAffinityUpstream;
NSRange oldSelectedRange = [(FlutterTextRange*)self.selectedTextRange range];
if (selectedRange.location != oldSelectedRange.location ||
selectedRange.length != oldSelectedRange.length) {
[self.inputDelegate selectionWillChange:self];
[self setSelectedTextRange:[FlutterTextRange rangeWithNSRange:selectedRange]
updateEditingState:NO];
_selectionAffinity = _kTextAffinityDownstream;
if ([state[@"selectionAffinity"] isEqualToString:@(_kTextAffinityUpstream)])
_selectionAffinity = _kTextAffinityUpstream;
[self.inputDelegate selectionDidChange:self];
}
[self.inputDelegate selectionDidChange:self];
[self.inputDelegate textDidChange:self];
if (textChanged)
[self.inputDelegate textDidChange:self];
}
#pragma mark - UIResponder Overrides
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册