RCTBackedTextInputDelegateAdapter.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <React/RCTBackedTextInputDelegateAdapter.h>
  8. #pragma mark - RCTBackedTextFieldDelegateAdapter (for UITextField)
  9. static void *TextFieldSelectionObservingContext = &TextFieldSelectionObservingContext;
  10. @interface RCTBackedTextFieldDelegateAdapter () <UITextFieldDelegate>
  11. @end
  12. @implementation RCTBackedTextFieldDelegateAdapter {
  13. __weak UITextField<RCTBackedTextInputViewProtocol> *_backedTextInputView;
  14. BOOL _textDidChangeIsComing;
  15. UITextRange *_previousSelectedTextRange;
  16. }
  17. - (instancetype)initWithTextField:(UITextField<RCTBackedTextInputViewProtocol> *)backedTextInputView
  18. {
  19. if (self = [super init]) {
  20. _backedTextInputView = backedTextInputView;
  21. backedTextInputView.delegate = self;
  22. [_backedTextInputView addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
  23. [_backedTextInputView addTarget:self action:@selector(textFieldDidEndEditingOnExit) forControlEvents:UIControlEventEditingDidEndOnExit];
  24. }
  25. return self;
  26. }
  27. - (void)dealloc
  28. {
  29. [_backedTextInputView removeTarget:self action:nil forControlEvents:UIControlEventEditingChanged];
  30. [_backedTextInputView removeTarget:self action:nil forControlEvents:UIControlEventEditingDidEndOnExit];
  31. }
  32. #pragma mark - UITextFieldDelegate
  33. - (BOOL)textFieldShouldBeginEditing:(__unused UITextField *)textField
  34. {
  35. return [_backedTextInputView.textInputDelegate textInputShouldBeginEditing];
  36. }
  37. - (void)textFieldDidBeginEditing:(__unused UITextField *)textField
  38. {
  39. [_backedTextInputView.textInputDelegate textInputDidBeginEditing];
  40. }
  41. - (BOOL)textFieldShouldEndEditing:(__unused UITextField *)textField
  42. {
  43. return [_backedTextInputView.textInputDelegate textInputShouldEndEditing];
  44. }
  45. - (void)textFieldDidEndEditing:(__unused UITextField *)textField
  46. {
  47. if (_textDidChangeIsComing) {
  48. // iOS does't call `textViewDidChange:` delegate method if the change was happened because of autocorrection
  49. // which was triggered by losing focus. So, we call it manually.
  50. _textDidChangeIsComing = NO;
  51. [_backedTextInputView.textInputDelegate textInputDidChange];
  52. }
  53. [_backedTextInputView.textInputDelegate textInputDidEndEditing];
  54. }
  55. - (BOOL)textField:(__unused UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  56. {
  57. NSString *newText =
  58. [_backedTextInputView.textInputDelegate textInputShouldChangeText:string inRange:range];
  59. if (newText == nil) {
  60. return NO;
  61. }
  62. if ([newText isEqualToString:string]) {
  63. _textDidChangeIsComing = YES;
  64. return YES;
  65. }
  66. NSMutableAttributedString *attributedString = [_backedTextInputView.attributedText mutableCopy];
  67. [attributedString replaceCharactersInRange:range withString:newText];
  68. [_backedTextInputView setAttributedText:[attributedString copy]];
  69. // Setting selection to the end of the replaced text.
  70. UITextPosition *position =
  71. [_backedTextInputView positionFromPosition:_backedTextInputView.beginningOfDocument
  72. offset:(range.location + newText.length)];
  73. [_backedTextInputView setSelectedTextRange:[_backedTextInputView textRangeFromPosition:position toPosition:position]
  74. notifyDelegate:YES];
  75. [self textFieldDidChange];
  76. return NO;
  77. }
  78. - (BOOL)textFieldShouldReturn:(__unused UITextField *)textField
  79. {
  80. return [_backedTextInputView.textInputDelegate textInputShouldReturn];
  81. }
  82. #pragma mark - UIControlEventEditing* Family Events
  83. - (void)textFieldDidChange
  84. {
  85. _textDidChangeIsComing = NO;
  86. [_backedTextInputView.textInputDelegate textInputDidChange];
  87. // `selectedTextRangeWasSet` isn't triggered during typing.
  88. [self textFieldProbablyDidChangeSelection];
  89. }
  90. - (void)textFieldDidEndEditingOnExit
  91. {
  92. [_backedTextInputView.textInputDelegate textInputDidReturn];
  93. }
  94. #pragma mark - UIKeyboardInput (private UIKit protocol)
  95. // This method allows us to detect a [Backspace] `keyPress`
  96. // even when there is no more text in the `UITextField`.
  97. - (BOOL)keyboardInputShouldDelete:(__unused UITextField *)textField
  98. {
  99. [_backedTextInputView.textInputDelegate textInputShouldChangeText:@"" inRange:NSMakeRange(0, 0)];
  100. return YES;
  101. }
  102. #pragma mark - Public Interface
  103. - (void)skipNextTextInputDidChangeSelectionEventWithTextRange:(UITextRange *)textRange
  104. {
  105. _previousSelectedTextRange = textRange;
  106. }
  107. - (void)selectedTextRangeWasSet
  108. {
  109. [self textFieldProbablyDidChangeSelection];
  110. }
  111. #pragma mark - Generalization
  112. - (void)textFieldProbablyDidChangeSelection
  113. {
  114. if ([_backedTextInputView.selectedTextRange isEqual:_previousSelectedTextRange]) {
  115. return;
  116. }
  117. _previousSelectedTextRange = _backedTextInputView.selectedTextRange;
  118. [_backedTextInputView.textInputDelegate textInputDidChangeSelection];
  119. }
  120. @end
  121. #pragma mark - RCTBackedTextViewDelegateAdapter (for UITextView)
  122. @interface RCTBackedTextViewDelegateAdapter () <UITextViewDelegate>
  123. @end
  124. @implementation RCTBackedTextViewDelegateAdapter {
  125. __weak UITextView<RCTBackedTextInputViewProtocol> *_backedTextInputView;
  126. BOOL _textDidChangeIsComing;
  127. UITextRange *_previousSelectedTextRange;
  128. }
  129. - (instancetype)initWithTextView:(UITextView<RCTBackedTextInputViewProtocol> *)backedTextInputView
  130. {
  131. if (self = [super init]) {
  132. _backedTextInputView = backedTextInputView;
  133. backedTextInputView.delegate = self;
  134. }
  135. return self;
  136. }
  137. #pragma mark - UITextViewDelegate
  138. - (BOOL)textViewShouldBeginEditing:(__unused UITextView *)textView
  139. {
  140. return [_backedTextInputView.textInputDelegate textInputShouldBeginEditing];
  141. }
  142. - (void)textViewDidBeginEditing:(__unused UITextView *)textView
  143. {
  144. [_backedTextInputView.textInputDelegate textInputDidBeginEditing];
  145. }
  146. - (BOOL)textViewShouldEndEditing:(__unused UITextView *)textView
  147. {
  148. return [_backedTextInputView.textInputDelegate textInputShouldEndEditing];
  149. }
  150. - (void)textViewDidEndEditing:(__unused UITextView *)textView
  151. {
  152. if (_textDidChangeIsComing) {
  153. // iOS does't call `textViewDidChange:` delegate method if the change was happened because of autocorrection
  154. // which was triggered by losing focus. So, we call it manually.
  155. _textDidChangeIsComing = NO;
  156. [_backedTextInputView.textInputDelegate textInputDidChange];
  157. }
  158. [_backedTextInputView.textInputDelegate textInputDidEndEditing];
  159. }
  160. - (BOOL)textView:(__unused UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  161. {
  162. // Custom implementation of `textInputShouldReturn` and `textInputDidReturn` pair for `UITextView`.
  163. if (!_backedTextInputView.textWasPasted && [text isEqualToString:@"\n"]) {
  164. if ([_backedTextInputView.textInputDelegate textInputShouldReturn]) {
  165. [_backedTextInputView.textInputDelegate textInputDidReturn];
  166. [_backedTextInputView endEditing:NO];
  167. return NO;
  168. }
  169. }
  170. NSString *newText =
  171. [_backedTextInputView.textInputDelegate textInputShouldChangeText:text inRange:range];
  172. if (newText == nil) {
  173. return NO;
  174. }
  175. if ([newText isEqualToString:text]) {
  176. _textDidChangeIsComing = YES;
  177. return YES;
  178. }
  179. NSMutableAttributedString *attributedString = [_backedTextInputView.attributedText mutableCopy];
  180. [attributedString replaceCharactersInRange:range withString:newText];
  181. [_backedTextInputView setAttributedText:[attributedString copy]];
  182. // Setting selection to the end of the replaced text.
  183. UITextPosition *position =
  184. [_backedTextInputView positionFromPosition:_backedTextInputView.beginningOfDocument
  185. offset:(range.location + newText.length)];
  186. [_backedTextInputView setSelectedTextRange:[_backedTextInputView textRangeFromPosition:position toPosition:position]
  187. notifyDelegate:YES];
  188. [self textViewDidChange:_backedTextInputView];
  189. return NO;
  190. }
  191. - (void)textViewDidChange:(__unused UITextView *)textView
  192. {
  193. _textDidChangeIsComing = NO;
  194. [_backedTextInputView.textInputDelegate textInputDidChange];
  195. }
  196. - (void)textViewDidChangeSelection:(__unused UITextView *)textView
  197. {
  198. [self textViewProbablyDidChangeSelection];
  199. }
  200. #pragma mark - UIScrollViewDelegate
  201. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  202. {
  203. if ([_backedTextInputView.textInputDelegate respondsToSelector:@selector(scrollViewDidScroll:)]) {
  204. [_backedTextInputView.textInputDelegate scrollViewDidScroll:scrollView];
  205. }
  206. }
  207. #pragma mark - Public Interface
  208. - (void)skipNextTextInputDidChangeSelectionEventWithTextRange:(UITextRange *)textRange
  209. {
  210. _previousSelectedTextRange = textRange;
  211. }
  212. #pragma mark - Generalization
  213. - (void)textViewProbablyDidChangeSelection
  214. {
  215. if ([_backedTextInputView.selectedTextRange isEqual:_previousSelectedTextRange]) {
  216. return;
  217. }
  218. _previousSelectedTextRange = _backedTextInputView.selectedTextRange;
  219. [_backedTextInputView.textInputDelegate textInputDidChangeSelection];
  220. }
  221. @end