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
This commit is contained in:
zhongwuzw 2019-05-07 03:48:11 -07:00 коммит произвёл Facebook Github Bot
Родитель 70e2ab2ec9
Коммит fc8008e0b2
1 изменённых файлов: 12 добавлений и 0 удалений

Просмотреть файл

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