Skip to content
Permalink
Browse files

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: #24714

Differential Revision: D15238379

Pulled By: cpojer

fbshipit-source-id: f149721d6b4df28e90f5a9405c74e01fde7c7d10
  • Loading branch information...
zhongwuzw authored and facebook-github-bot committed May 7, 2019
1 parent 70e2ab2 commit fc8008e0b2d975287a6867a84beb0d4123b4f83f
Showing with 12 additions and 0 deletions.
  1. +12 −0 Libraries/Text/TextInput/RCTBackedTextInputDelegateAdapter.m
@@ -28,15 +28,27 @@ - (instancetype)initWithTextField:(UITextField<RCTBackedTextInputViewProtocol> *

[_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

0 comments on commit fc8008e

Please sign in to comment.
You can’t perform that action at this time.