Skip to content

Commit

Permalink
Add cursor coords to text input selection.
Browse files Browse the repository at this point in the history
iOS impl only, for usage in building a multiline text input that avoids the keyboard. https://medium.com/@p9alisand/how-to-get-textinput-cursor-position-in-react-native-0-49-1845aee40e9b
  • Loading branch information
wilimitis committed May 12, 2018
1 parent 2563503 commit 9cb1411
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Libraries/Text/TextInput/RCTBaseTextInputView.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ - (RCTTextSelection *)selection
id<RCTBackedTextInputViewProtocol> backedTextInput = self.backedTextInputView;
UITextRange *selectedTextRange = backedTextInput.selectedTextRange;
return [[RCTTextSelection new] initWithStart:[backedTextInput offsetFromPosition:backedTextInput.beginningOfDocument toPosition:selectedTextRange.start]
end:[backedTextInput offsetFromPosition:backedTextInput.beginningOfDocument toPosition:selectedTextRange.end]];
end:[backedTextInput offsetFromPosition:backedTextInput.beginningOfDocument toPosition:selectedTextRange.end]
cursorPosition:[backedTextInput caretRectForPosition:selectedTextRange.start].origin];
}

- (void)setSelection:(RCTTextSelection *)selection
Expand Down Expand Up @@ -162,6 +163,8 @@ - (void)textInputDidChangeSelection
@"selection": @{
@"start": @(selection.start),
@"end": @(selection.end),
@"cursorPositionX": @(selection.cursorPosition.x),
@"cursorPositionY": @(selection.cursorPosition.y),
},
});
}
Expand Down
3 changes: 2 additions & 1 deletion Libraries/Text/TextInput/RCTTextSelection.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

@property (nonatomic, assign, readonly) NSInteger start;
@property (nonatomic, assign, readonly) NSInteger end;
@property (nonatomic, assign, readonly) CGPoint cursorPosition;

- (instancetype)initWithStart:(NSInteger)start end:(NSInteger)end;
- (instancetype)initWithStart:(NSInteger)start end:(NSInteger)end cursorPosition:(CGPoint)cursorPosition;

@end

Expand Down
11 changes: 9 additions & 2 deletions Libraries/Text/TextInput/RCTTextSelection.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@

@implementation RCTTextSelection

- (instancetype)initWithStart:(NSInteger)start end:(NSInteger)end
- (instancetype)initWithStart:(NSInteger)start end:(NSInteger)end cursorPosition:(CGPoint)cursorPosition
{
if (self = [super init]) {
_start = start;
_end = end;
_cursorPosition = cursorPosition;
}
return self;
}
Expand All @@ -29,7 +30,13 @@ + (RCTTextSelection *)RCTTextSelection:(id)json
if ([json isKindOfClass:[NSDictionary class]]) {
NSInteger start = [self NSInteger:json[@"start"]];
NSInteger end = [self NSInteger:json[@"end"]];
return [[RCTTextSelection alloc] initWithStart:start end:end];
CGPoint cursorPosition = CGPointMake(
[self CGFloat:json[@"cursorPositionX"]],
[self CGFloat:json[@"cursorPositionY"]]
);
return [[RCTTextSelection alloc] initWithStart:start
end:end
cursorPosition:cursorPosition];
}

return nil;
Expand Down

0 comments on commit 9cb1411

Please sign in to comment.