From b9c8dfb635bf8e3e32616a76498eb339139f96e1 Mon Sep 17 00:00:00 2001 From: Taras Perun Date: Fri, 24 Mar 2023 16:48:37 +0100 Subject: [PATCH] ios cursor position --- Libraries/Text/TextInput/RCTBaseTextInputView.m | 8 +++++++- Libraries/Text/TextInput/RCTTextSelection.h | 3 ++- Libraries/Text/TextInput/RCTTextSelection.m | 11 +++++++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.m b/Libraries/Text/TextInput/RCTBaseTextInputView.m index def2a04d5c5809..0842311db01a92 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.m @@ -176,7 +176,8 @@ - (RCTTextSelection *)selection id backedTextInputView = self.backedTextInputView; UITextRange *selectedTextRange = backedTextInputView.selectedTextRange; return [[RCTTextSelection new] initWithStart:[backedTextInputView offsetFromPosition:backedTextInputView.beginningOfDocument toPosition:selectedTextRange.start] - end:[backedTextInputView offsetFromPosition:backedTextInputView.beginningOfDocument toPosition:selectedTextRange.end]]; + end:[backedTextInputView offsetFromPosition:backedTextInputView.beginningOfDocument toPosition:selectedTextRange.end] + cursorPosition:[backedTextInputView caretRectForPosition:selectedTextRange.start].origin]; } - (void)setSelection:(RCTTextSelection *)selection @@ -491,10 +492,15 @@ - (void)textInputDidChangeSelection RCTTextSelection *selection = self.selection; + UITextRange *selectedTextRange = self.backedTextInputView.selectedTextRange; + CGPoint selectionOrigin = [self.backedTextInputView caretRectForPosition:selectedTextRange.start].origin; + _onSelectionChange(@{ @"selection": @{ @"start": @(selection.start), @"end": @(selection.end), + @"positionY": @(selectionOrigin.y), + @"positionX": @(selectionOrigin.x), }, }); } diff --git a/Libraries/Text/TextInput/RCTTextSelection.h b/Libraries/Text/TextInput/RCTTextSelection.h index baa2e0e46a1a23..30970290401922 100644 --- a/Libraries/Text/TextInput/RCTTextSelection.h +++ b/Libraries/Text/TextInput/RCTTextSelection.h @@ -14,8 +14,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 diff --git a/Libraries/Text/TextInput/RCTTextSelection.m b/Libraries/Text/TextInput/RCTTextSelection.m index bbcf63bf0bccd1..915438d5bbcdbc 100644 --- a/Libraries/Text/TextInput/RCTTextSelection.m +++ b/Libraries/Text/TextInput/RCTTextSelection.m @@ -9,11 +9,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; } @@ -27,7 +28,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;