Skip to content

Commit 8bfc166

Browse files
authored
Support inline views in Text with lineHeight on iOS (#59899)
Summary: Fixes the issue described in [#48727](#48727) on iOS, where adding inline views and images into Text when line height is specified results in the image clipping and overflowing the parent container. ### Root Cause On iOS, `lineHeight` sets both `minimumLineHeight` and `maximumLineHeight` so TextKit clamps every line to the same height, even for lines with a taller inline view/image. ### This PR For iOS, set `RCTTextLayoutManager` as a `NSLayoutManagerDelegate` if an inline view exists so the layout manager calls `layoutManagerShouldSetLineFragmentRect()` which grows the line to match the height of the inline view. All changes are gated behind the `fixTextInlineViewLineHeight` flag. Changelog: [iOS][Fixed] - Inline views in Text with lineHeight no longer overflow the text Differential Revision: D130190418
1 parent 9fcb0ee commit 8bfc166

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

  • packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager

‎packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
using namespace facebook::react;
2323

24+
@interface RCTTextLayoutManager () <NSLayoutManagerDelegate>
25+
@end
26+
2427
@implementation RCTTextLayoutManager {
2528
SimpleThreadSafeCache<AttributedString, std::shared_ptr<void>, 256> _cache;
2629
}
@@ -457,6 +460,10 @@ - (NSTextStorage *)_textStorageAndLayoutManagerWithAttributesString:(NSAttribute
457460

458461
NSLayoutManager *layoutManager = [NSLayoutManager new];
459462
layoutManager.usesFontLeading = NO;
463+
if (ReactNativeFeatureFlags::fixTextInlineViewLineHeight() &&
464+
[attributedString containsAttachmentsInRange:NSMakeRange(0, attributedString.length)]) {
465+
layoutManager.delegate = self;
466+
}
460467
[layoutManager addTextContainer:textContainer];
461468

462469
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString];
@@ -479,6 +486,47 @@ - (NSTextStorage *)_textStorageAndLayoutManagerWithAttributesString:(NSAttribute
479486
return textStorage;
480487
}
481488

489+
#pragma mark - NSLayoutManagerDelegate
490+
491+
- (BOOL)layoutManager:(NSLayoutManager *)layoutManager
492+
shouldSetLineFragmentRect:(inout CGRect *)lineFragmentRect
493+
lineFragmentUsedRect:(inout CGRect *)lineFragmentUsedRect
494+
baselineOffset:(inout CGFloat *)baselineOffset
495+
inTextContainer:(NSTextContainer *)textContainer
496+
forGlyphRange:(NSRange)glyphRange
497+
{
498+
NSTextStorage *textStorage = layoutManager.textStorage;
499+
NSRange characterRange = [layoutManager characterRangeForGlyphRange:glyphRange actualGlyphRange:nil];
500+
501+
// Tallest space any attachment on this line needs above the baseline, counting the NSBaselineOffset lift that
502+
// RCTApplyBaselineOffset gives the text.
503+
__block CGFloat requiredAscent = 0;
504+
[textStorage enumerateAttribute:NSAttachmentAttributeName
505+
inRange:characterRange
506+
options:0
507+
usingBlock:^(NSTextAttachment *attachment, NSRange range, __unused BOOL *stop) {
508+
if (attachment == nil) {
509+
return;
510+
}
511+
CGFloat baselineShift = [[textStorage attribute:NSBaselineOffsetAttributeName
512+
atIndex:range.location
513+
effectiveRange:nil] doubleValue];
514+
requiredAscent = MAX(attachment.bounds.size.height + baselineShift, requiredAscent);
515+
}];
516+
517+
// maximumLineHeight (from lineHeight) caps the line even when an attachment is taller.
518+
// Inline views aren't subject to lineHeight, so add the missing height above the baseline.
519+
CGFloat missingHeight = requiredAscent - *baselineOffset;
520+
if (missingHeight <= 0) {
521+
return NO;
522+
}
523+
524+
lineFragmentRect->size.height += missingHeight;
525+
lineFragmentUsedRect->size.height += missingHeight;
526+
*baselineOffset += missingHeight;
527+
return YES;
528+
}
529+
482530
- (std::shared_ptr<const EventEmitter>)getEventEmitterWithAttributeString:(const AttributedString &)attributedString
483531
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
484532
frame:(CGRect)frame

0 commit comments

Comments
 (0)