Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
face14d
feat: implement selectable text view and update text layout manager f…
raajnadar Sep 6, 2026
bfb22e7
fix: add textStorageForNSAttributedString method to RCTTextLayoutManager
raajnadar Sep 7, 2026
d185a90
fix: enhance RCTSelectableTextView for improved text selection handling
raajnadar Sep 12, 2026
2dbe5b7
fix: add sourceAttributedText property and update copy method in RCTS…
raajnadar Sep 12, 2026
ef3e8d7
Merge branch 'main' into fix/ios-partial-text-selection
raajnadar Sep 18, 2026
a401293
Merge remote-tracking branch 'upstream/main' into fix/ios-partial-tex…
raajnadar Sep 18, 2026
061745c
remove redundant comments in RCTParagraphComponentView.mm for clarity
raajnadar Sep 19, 2026
1c80a02
fix: text selection handling in RCTParagraphComponentView and add tes…
raajnadar Sep 19, 2026
06858b5
fix: improve selection text view handling in recycled paragraphs and …
raajnadar Sep 25, 2026
cfff5db
feat: split RCTSelectableTextView to add it behind feature flag
raajnadar Oct 10, 2026
804fc55
Merge remote-tracking branch 'upstream/main' into fix/ios-partial-tex…
raajnadar Oct 10, 2026
ec3503a
fix: build the selectable text view in finalizeUpdates after layoutSu…
raajnadar Oct 10, 2026
d974ca1
feat: put partial text selection behind the enableIOSPartialTextSelec…
raajnadar Oct 10, 2026
44645fc
Merge remote-tracking branch 'upstream/main' into fix/ios-partial-tex…
raajnadar Oct 14, 2026
8c28b3f
Merge remote-tracking branch 'upstream/main' into fix/ios-partial-tex…
raajnadar Oct 16, 2026
089b0b1
Merge remote-tracking branch 'upstream/main' into fix/ios-partial-tex…
raajnadar Oct 23, 2026
fdb5bc7
Merge branch 'main' of https://github.com/react/react-native into fix…
raajnadar Oct 31, 2026
354dbf4
test(iOS): prove the selection text view strips native painting attri…
raajnadar Nov 1, 2026
cf789ab
Merge remote-tracking branch 'upstream/main' into fix/ios-partial-tex…
raajnadar Nov 4, 2026
8aff841
fix: build the selection text view on the first touch
raajnadar Nov 4, 2026
bff1c36
Merge remote-tracking branch 'upstream/main' into fix/ios-partial-tex…
raajnadar Nov 7, 2026
1355738
fix: lay out the selection text in the same container size as the dra…
raajnadar Nov 7, 2026
4e07d1e
fix: cancel only the selection touch and scope the emitter guard to t…
raajnadar Nov 7, 2026
5913659
fix: enhance paragraph selection tests for layout consistency and fon…
raajnadar Nov 7, 2026
422d8c0
fix: keep a touch on the selection handles out of React Native from s…
raajnadar Nov 7, 2026
e4cf607
Merge remote-tracking branch 'upstream/main' into fix/ios-partial-tex…
raajnadar Nov 15, 2026
799b274
fix: clear the selection on a tap on an inline view
raajnadar Nov 15, 2026
ee174cf
Merge remote-tracking branch 'upstream/main' into fix/ios-partial-tex…
raajnadar Nov 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ - (CGRect)drawingFrameForAttributedString:(facebook::react::AttributedString)att
frame:(CGRect)frame
containerFrame:(CGRect *)containerFrame;

- (NSTextStorage *)textStorageForNSAttributedString:(NSAttributedString *)attributedString
paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes
size:(CGSize)size;

@end

// ParagraphTextView is an auxiliary view we set as contentView so the drawing
Expand All @@ -46,22 +50,60 @@ @interface RCTParagraphTextView : UIView
@end

#if !TARGET_OS_TV
@interface RCTParagraphComponentView () <UIEditMenuInteractionDelegate>
/*
* A non-editable `UITextView` used to render a selectable paragraph.
*
* It is created with the very `NSTextContainer` that `RCTTextLayoutManager`
* measured the paragraph with, so its layout matches the measurement by
* construction rather than by coincidence. UIKit performs the selection; it
* never performs the layout.
*/
@interface RCTSelectableTextView : UITextView
@end

@implementation RCTSelectableTextView

@property (nonatomic, nullable) UIEditMenuInteraction *editMenuInteraction API_AVAILABLE(ios(16.0));
- (instancetype)initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)textContainer
{
if (self = [super initWithFrame:frame textContainer:textContainer]) {
self.backgroundColor = UIColor.clearColor;
self.editable = NO;
self.selectable = YES;
self.scrollEnabled = NO;
self.contentInset = UIEdgeInsetsZero;
self.textContainerInset = UIEdgeInsetsZero;
self.adjustsFontForContentSizeCategory = NO;
// `RCTTextLayoutManager` already applies the padding it wants.
self.textContainer.lineFragmentPadding = 0.0;
// The paragraph owns its layout; the text view must never reflow it.
self.textContainer.widthTracksTextView = NO;
self.textContainer.heightTracksTextView = NO;
// <Paragraph> publishes its own accessibility elements, one per link, through
// `RCTParagraphComponentAccessibilityProvider`. Keeping the text view out of
// the accessibility tree leaves that contract exactly as it was.
self.accessibilityElementsHidden = YES;
}
return self;
}

@end
#else
#endif // !TARGET_OS_TV

@interface RCTParagraphComponentView ()
@end
#endif

@implementation RCTParagraphComponentView {
ParagraphAttributes _paragraphAttributes;
RCTParagraphComponentAccessibilityProvider *_accessibilityProvider;
UILongPressGestureRecognizer *_longPressGestureRecognizer;
RCTParagraphTextView *_textView;
CGRect _textLayoutFrame;
#if !TARGET_OS_TV
// Selection state. `_selectableTextView` is non-nil only while `selectable` is set.
RCTSelectableTextView *_selectableTextView;
RCTTextLayoutManager *_selectionLayoutManager;
NSAttributedString *_selectionRenderedText;
CGSize _selectionRenderedSize;
#endif
}

- (instancetype)initWithFrame:(CGRect)frame
Expand Down Expand Up @@ -137,6 +179,9 @@ - (void)updateState:(const State::Shared &)state oldState:(const State::Shared &
{
_textView.state = std::static_pointer_cast<const ParagraphShadowNode::ConcreteState>(state);
[_textView setNeedsDisplay];
#if !TARGET_OS_TV
_selectionRenderedText = nil;
#endif
[self setNeedsLayout];

// If the attributed string has changed, we need to notify the accessibility system that something changed,
Expand Down Expand Up @@ -168,6 +213,9 @@ - (void)prepareForRecycle
[super prepareForRecycle];
_textView.state = nullptr;
_accessibilityProvider = nil;
#if !TARGET_OS_TV
[self disableContextMenu];
#endif
}

- (void)layoutSubviews
Expand Down Expand Up @@ -196,6 +244,13 @@ - (void)layoutSubviews
_textLayoutFrame = drawingFrame;
_textView.frame = textViewFrame;
_textView.drawingFrame = CGRectOffset(drawingFrame, -textViewFrame.origin.x, -textViewFrame.origin.y);

#if !TARGET_OS_TV
const auto &paragraphProps = static_cast<const ParagraphProps &>(*_props);
if (paragraphProps.isSelectable) {
[self updateSelectableTextViewWithFrame:RCTCGRectFromRect(_layoutMetrics.getContentFrame())];
}
#endif
}

#pragma mark - Accessibility
Expand Down Expand Up @@ -332,83 +387,75 @@ - (SharedTouchEventEmitter)touchEventEmitterAtPoint:(CGPoint)point
#pragma mark - Context Menu

#if !TARGET_OS_TV
/*
* Selection is provided by a `UITextView` laid out with the paragraph's own
* TextKit stack, which gives the platform behaviour users expect: long press to
* select a word, drag handles to extend the range and an edit menu that copies
* only what is selected.
*/
- (void)enableContextMenu
{
_longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(handleLongPress:)];

if (@available(iOS 16.0, *)) {
_editMenuInteraction = [[UIEditMenuInteraction alloc] initWithDelegate:self];
[self addInteraction:_editMenuInteraction];
if (_selectionLayoutManager == nil) {
_selectionLayoutManager = [RCTTextLayoutManager new];
}
[self addGestureRecognizer:_longPressGestureRecognizer];
_selectionRenderedText = nil;
[self setNeedsLayout];
}

- (void)disableContextMenu
{
[self removeGestureRecognizer:_longPressGestureRecognizer];
if (@available(iOS 16.0, *)) {
[self removeInteraction:_editMenuInteraction];
_editMenuInteraction = nil;
}
_longPressGestureRecognizer = nil;
[_selectableTextView removeFromSuperview];
_selectableTextView = nil;
_selectionRenderedText = nil;
_selectionRenderedSize = CGSizeZero;
_textView.hidden = NO;
}

- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture
/*
* Builds or repositions the selectable text view. A `UITextView` binds its text
* container at initialisation, so it is rebuilt only when the text or the
* available size actually changes.
*/
- (void)updateSelectableTextViewWithFrame:(CGRect)contentFrame
{
if (@available(iOS 16.0, macCatalyst 16.0, *)) {
CGPoint location = [gesture locationInView:self];
UIEditMenuConfiguration *config = [UIEditMenuConfiguration configurationWithIdentifier:nil sourcePoint:location];
if (_editMenuInteraction) {
[_editMenuInteraction presentEditMenuWithConfiguration:config];
}
} else {
UIMenuController *menuController = [UIMenuController sharedMenuController];

if (menuController.isMenuVisible) {
return;
}

[menuController showMenuFromView:self rect:self.bounds];
NSAttributedString *attributedText = self.attributedText;
if (attributedText == nil || CGRectIsEmpty(contentFrame)) {
[_selectableTextView removeFromSuperview];
_selectableTextView = nil;
_selectionRenderedText = nil;
_textView.hidden = NO;
return;
}
}

- (BOOL)canBecomeFirstResponder
{
const auto &paragraphProps = static_cast<const ParagraphProps &>(*_props);
return paragraphProps.isSelectable;
}
BOOL needsRebuild = _selectableTextView == nil ||
![attributedText isEqualToAttributedString:_selectionRenderedText] ||
!CGSizeEqualToSize(contentFrame.size, _selectionRenderedSize);

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
const auto &paragraphProps = static_cast<const ParagraphProps &>(*_props);
if (needsRebuild) {
NSTextStorage *textStorage = [_selectionLayoutManager textStorageForNSAttributedString:attributedText
paragraphAttributes:_paragraphAttributes
size:contentFrame.size];
NSTextContainer *textContainer = textStorage.layoutManagers.firstObject.textContainers.firstObject;

if (paragraphProps.isSelectable && action == @selector(copy:)) {
return YES;
[_selectableTextView removeFromSuperview];
_selectableTextView = [[RCTSelectableTextView alloc] initWithFrame:contentFrame textContainer:textContainer];
[self addSubview:_selectableTextView];

_selectionRenderedText = [attributedText copy];
_selectionRenderedSize = contentFrame.size;
}

return [self.nextResponder canPerformAction:action withSender:sender];
_selectableTextView.frame = contentFrame;
// The text view renders the paragraph, so the drawn copy must stay hidden.
Comment thread
raajnadar marked this conversation as resolved.
Resolved
_textView.hidden = YES;
}

- (void)copy:(id)sender
- (BOOL)canBecomeFirstResponder
{
NSAttributedString *attributedText = self.attributedText;

NSMutableDictionary *item = [NSMutableDictionary new];

NSData *rtf = [attributedText dataFromRange:NSMakeRange(0, attributedText.length)
documentAttributes:@{NSDocumentTypeDocumentAttribute : NSRTFDTextDocumentType}
error:nil];

if (rtf) {
[item setObject:rtf forKey:(id)kUTTypeFlatRTFD];
}

[item setObject:attributedText.string forKey:(id)kUTTypeUTF8PlainText];

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.items = @[ item ];
// While selectable, `_selectableTextView` is the responder that owns the selection.
return NO;
}

#else
- (void)enableContextMenu
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,15 @@ - (LinesMeasurements)getLinesForAttributedString:(facebook::react::AttributedStr
return paragraphLines;
}

- (NSTextStorage *)textStorageForNSAttributedString:(NSAttributedString *)attributedString
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
size:(CGSize)size
{
return [self _textStorageAndLayoutManagerWithAttributesString:attributedString
paragraphAttributes:paragraphAttributes
size:size];
}

- (NSTextStorage *)_textStorageAndLayoutManagerWithAttributesString:(NSAttributedString *)attributedString
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
size:(CGSize)size
Expand Down
Loading