|
13 | 13 | #import <React/RCTBackedTextInputDelegateAdapter.h> |
14 | 14 | #import <React/RCTTextAttributes.h> |
15 | 15 |
|
| 16 | +//the UITextSelectionRect subclass needs to be created because the original version is not writable |
| 17 | +@interface CustomTextSelectionRect : UITextSelectionRect |
| 18 | + |
| 19 | +@property (nonatomic) CGRect _rect; |
| 20 | +@property (nonatomic) NSWritingDirection _writingDirection; |
| 21 | +@property (nonatomic) BOOL _containsStart; // Returns YES if the rect contains the start of the selection. |
| 22 | +@property (nonatomic) BOOL _containsEnd; // Returns YES if the rect contains the end of the selection. |
| 23 | +@property (nonatomic) BOOL _isVertical; // Returns YES if the rect is for vertically oriented text. |
| 24 | + |
| 25 | +@end |
| 26 | + |
| 27 | +@implementation CustomTextSelectionRect |
| 28 | + |
| 29 | +- (CGRect)rect { |
| 30 | + return __rect; |
| 31 | +} |
| 32 | + |
| 33 | +- (NSWritingDirection)writingDirection { |
| 34 | + return __writingDirection; |
| 35 | +} |
| 36 | + |
| 37 | +- (BOOL)containsStart { |
| 38 | + return __containsStart; |
| 39 | +} |
| 40 | + |
| 41 | +- (BOOL)containsEnd { |
| 42 | + return __containsEnd; |
| 43 | +} |
| 44 | + |
| 45 | +- (BOOL)isVertical { |
| 46 | + return __isVertical; |
| 47 | +} |
| 48 | + |
| 49 | +@end |
| 50 | + |
16 | 51 | @implementation RCTUITextView { |
17 | 52 | UILabel *_placeholderView; |
18 | 53 | UITextView *_detachedTextView; |
@@ -294,11 +329,43 @@ - (void)_updatePlaceholder |
294 | 329 |
|
295 | 330 | - (CGRect)caretRectForPosition:(UITextPosition *)position |
296 | 331 | { |
| 332 | + CGRect originalRect = [super caretRectForPosition:position]; |
| 333 | + |
297 | 334 | if (_caretHidden) { |
298 | 335 | return CGRectZero; |
299 | 336 | } |
300 | 337 |
|
301 | | - return [super caretRectForPosition:position]; |
| 338 | + if(_caretYOffset != 0) { |
| 339 | + originalRect.origin.y += _caretYOffset; |
| 340 | + } |
| 341 | + |
| 342 | + if(_caretHeight != 0) { |
| 343 | + originalRect.size.height = _caretHeight; |
| 344 | + } |
| 345 | + return originalRect; |
| 346 | +} |
| 347 | + |
| 348 | +- (NSArray *)selectionRectsForRange:(UITextRange *)range { |
| 349 | + NSArray *superRects = [super selectionRectsForRange:range]; |
| 350 | + if(_caretYOffset != 0 && _caretHeight != 0) { |
| 351 | + NSMutableArray *customTextSelectionRects = [NSMutableArray array]; |
| 352 | + |
| 353 | + for (UITextSelectionRect *rect in superRects) { |
| 354 | + CustomTextSelectionRect *customTextRect = [[CustomTextSelectionRect alloc] init]; |
| 355 | + |
| 356 | + customTextRect._rect = CGRectMake(rect.rect.origin.x, rect.rect.origin.y + _caretYOffset, rect.rect.size.width, _caretHeight); |
| 357 | + customTextRect._writingDirection = rect.writingDirection; |
| 358 | + customTextRect._containsStart = rect.containsStart; |
| 359 | + customTextRect._containsEnd = rect.containsEnd; |
| 360 | + customTextRect._isVertical = rect.isVertical; |
| 361 | + [customTextSelectionRects addObject:customTextRect]; |
| 362 | + } |
| 363 | + |
| 364 | + return customTextSelectionRects; |
| 365 | + |
| 366 | + } |
| 367 | + return superRects; |
| 368 | + |
302 | 369 | } |
303 | 370 |
|
304 | 371 | #pragma mark - Utility Methods |
|
0 commit comments