From fc8008e0b2d975287a6867a84beb0d4123b4f83f Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Tue, 7 May 2019 03:48:11 -0700 Subject: [PATCH] Fixes selection of single line text input (#24714) Summary: `UITextField` don't have delegate to observe the selection changes of users(multiline text input `UITextView` have it), so we can add a KVO to observe selection changes. cpojer shergin [iOS] [Fixed] - Fixes selection of single line text input Pull Request resolved: https://github.com/facebook/react-native/pull/24714 Differential Revision: D15238379 Pulled By: cpojer fbshipit-source-id: f149721d6b4df28e90f5a9405c74e01fde7c7d10 --- .../TextInput/RCTBackedTextInputDelegateAdapter.m | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Libraries/Text/TextInput/RCTBackedTextInputDelegateAdapter.m b/Libraries/Text/TextInput/RCTBackedTextInputDelegateAdapter.m index fe5ee6764..8cbca2936 100644 --- a/Libraries/Text/TextInput/RCTBackedTextInputDelegateAdapter.m +++ b/Libraries/Text/TextInput/RCTBackedTextInputDelegateAdapter.m @@ -28,15 +28,27 @@ static void *TextFieldSelectionObservingContext = &TextFieldSelectionObservingCo [_backedTextInputView addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged]; [_backedTextInputView addTarget:self action:@selector(textFieldDidEndEditingOnExit) forControlEvents:UIControlEventEditingDidEndOnExit]; + [_backedTextInputView addObserver:self forKeyPath:@"selectedTextRange" options:NSKeyValueObservingOptionNew context:NULL]; } return self; } +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context +{ + // UITextField doesn't have a delegate like UITextView to get notified on selection. Use KVO to observe changes. + if ([keyPath isEqualToString:@"selectedTextRange"]) { + [self textFieldProbablyDidChangeSelection]; + } else { + [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; + } +} + - (void)dealloc { [_backedTextInputView removeTarget:self action:nil forControlEvents:UIControlEventEditingChanged]; [_backedTextInputView removeTarget:self action:nil forControlEvents:UIControlEventEditingDidEndOnExit]; + [_backedTextInputView removeObserver:self forKeyPath:@"selectedTextRange"]; } #pragma mark - UITextFieldDelegate