Skip to content
This repository was archived by the owner on Jun 24, 2021. It is now read-only.

Commit 35fa890

Browse files
Merge pull request #50 from mohamed-aleem/patch-1
fix selection on textView bug in iOS 13
2 parents fd8f8c1 + 0584727 commit 35fa890

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

ios/RNSelectableTextView.m

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ @implementation RNSelectableTextView
2727

2828
NSString *const CUSTOM_SELECTOR = @"_CUSTOM_SELECTOR_";
2929

30+
UITextPosition *selectionStart;
31+
UITextPosition* beginning;
32+
3033
- (instancetype)initWithBridge:(RCTBridge *)bridge
3134
{
3235
if (self = [super initWithBridge:bridge]) {
@@ -48,6 +51,8 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge
4851
_backedTextInputView.selectable = YES;
4952
_backedTextInputView.contextMenuHidden = YES;
5053

54+
beginning = _backedTextInputView.beginningOfDocument;
55+
5156
for (UIGestureRecognizer *gesture in [_backedTextInputView gestureRecognizers]) {
5257
if (
5358
[gesture isKindOfClass:[UIPanGestureRecognizer class]]
@@ -62,7 +67,8 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge
6267
[self addSubview:_backedTextInputView];
6368

6469
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
65-
70+
71+
6672
UITapGestureRecognizer *tapGesture = [ [UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
6773
tapGesture.numberOfTapsRequired = 2;
6874

@@ -128,15 +134,29 @@ -(void) handleSingleTap: (UITapGestureRecognizer *) gesture
128134

129135
-(void) handleLongPress: (UILongPressGestureRecognizer *) gesture
130136
{
137+
131138
CGPoint pos = [gesture locationInView:_backedTextInputView];
132139
pos.y += _backedTextInputView.contentOffset.y;
133-
140+
134141
UITextPosition *tapPos = [_backedTextInputView closestPositionToPoint:pos];
135142
UITextRange *word = [_backedTextInputView.tokenizer rangeEnclosingPosition:tapPos withGranularity:(UITextGranularityWord) inDirection:UITextLayoutDirectionRight];
136143

137-
UITextPosition* beginning = _backedTextInputView.beginningOfDocument;
138-
139-
UITextPosition *selectionStart = word.start;
144+
145+
switch ([gesture state]) {
146+
case UIGestureRecognizerStateBegan:
147+
selectionStart = word.start;
148+
break;
149+
case UIGestureRecognizerStateChanged:
150+
break;
151+
case UIGestureRecognizerStateEnded:
152+
selectionStart = nil;
153+
[self _handleGesture];
154+
return;
155+
156+
default:
157+
break;
158+
}
159+
140160
UITextPosition *selectionEnd = word.end;
141161

142162
const NSInteger location = [_backedTextInputView offsetFromPosition:beginning toPosition:selectionStart];
@@ -146,7 +166,7 @@ -(void) handleLongPress: (UILongPressGestureRecognizer *) gesture
146166

147167
[_backedTextInputView select:self];
148168
[_backedTextInputView setSelectedRange:NSMakeRange(location, endLocation - location)];
149-
[self _handleGesture];
169+
150170
}
151171

152172
-(void) handleTap: (UITapGestureRecognizer *) gesture
@@ -215,6 +235,7 @@ - (BOOL)canBecomeFirstResponder
215235

216236
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
217237
{
238+
if(selectionStart != nil) {return NO;}
218239
NSString *sel = NSStringFromSelector(action);
219240
NSRange match = [sel rangeOfString:CUSTOM_SELECTOR];
220241

@@ -226,28 +247,7 @@ - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
226247

227248
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
228249
{
229-
if (!_backedTextInputView.isFirstResponder) {
230-
[_backedTextInputView setSelectedTextRange:nil notifyDelegate:true];
231-
} else {
232-
UIView *sub = nil;
233-
for (UIView *subview in self.subviews.reverseObjectEnumerator) {
234-
CGPoint subPoint = [subview convertPoint:point toView:self];
235-
UIView *result = [subview hitTest:subPoint withEvent:event];
236-
237-
if (!result.isFirstResponder) {
238-
NSString *name = NSStringFromClass([result class]);
239-
240-
if ([name isEqual:@"UITextRangeView"]) {
241-
sub = result;
242-
}
243-
}
244-
}
245-
246-
if (sub == nil) {
247-
[_backedTextInputView setSelectedTextRange:nil notifyDelegate:true];
248-
}
249-
}
250-
250+
[_backedTextInputView setSelectedTextRange:nil notifyDelegate:true];
251251
return [super hitTest:point withEvent:event];
252252
}
253253

0 commit comments

Comments
 (0)