Skip to content

Commit

Permalink
Fixes selection of single line text input (#24714)
Browse files Browse the repository at this point in the history
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 fc8008e
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Libraries/Text/TextInput/RCTBackedTextInputDelegateAdapter.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,15 +28,27 @@ - (instancetype)initWithTextField:(UITextField<RCTBackedTextInputViewProtocol> *


[_backedTextInputView addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged]; [_backedTextInputView addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
[_backedTextInputView addTarget:self action:@selector(textFieldDidEndEditingOnExit) forControlEvents:UIControlEventEditingDidEndOnExit]; [_backedTextInputView addTarget:self action:@selector(textFieldDidEndEditingOnExit) forControlEvents:UIControlEventEditingDidEndOnExit];
[_backedTextInputView addObserver:self forKeyPath:@"selectedTextRange" options:NSKeyValueObservingOptionNew context:NULL];
} }


return self; 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 - (void)dealloc
{ {
[_backedTextInputView removeTarget:self action:nil forControlEvents:UIControlEventEditingChanged]; [_backedTextInputView removeTarget:self action:nil forControlEvents:UIControlEventEditingChanged];
[_backedTextInputView removeTarget:self action:nil forControlEvents:UIControlEventEditingDidEndOnExit]; [_backedTextInputView removeTarget:self action:nil forControlEvents:UIControlEventEditingDidEndOnExit];
[_backedTextInputView removeObserver:self forKeyPath:@"selectedTextRange"];
} }


#pragma mark - UITextFieldDelegate #pragma mark - UITextFieldDelegate
Expand Down

0 comments on commit fc8008e

Please sign in to comment.