Skip to content

Commit cfd4309

Browse files
mozziusclaude
andcommitted
Place views nested in Text on the baseline on iOS
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 34795cb commit cfd4309

1 file changed

Lines changed: 10 additions & 9 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: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -642,15 +642,16 @@ - (TextMeasurement)_measureTextStorage:(NSTextStorage *)textStorage
642642
CGSize attachmentSize = attachment.bounds.size;
643643
CGRect glyphRect = [layoutManager boundingRectForGlyphRange:range inTextContainer:textContainer];
644644

645-
CGRect frame;
646-
UIFont *font = [[textStorage attributedSubstringFromRange:range] attribute:NSFontAttributeName
647-
atIndex:0
648-
effectiveRange:nil];
649-
frame = {
650-
.origin =
651-
{glyphRect.origin.x,
652-
glyphRect.origin.y + glyphRect.size.height - attachmentSize.height + font.descender},
653-
.size = attachmentSize};
645+
// Sit the attachment on the baseline TextKit laid its line out with. That accounts for the offset
646+
// that centers glyphs within a custom `lineHeight`, and for the line's largest font. The
647+
// attachment's own run has no font (TextKit fills in 12pt Helvetica), so its descender can't be used.
648+
NSUInteger glyphIndex = attachmentGlyphRange.location;
649+
CGRect lineFragmentRect = [layoutManager lineFragmentRectForGlyphAtIndex:glyphIndex
650+
effectiveRange:NULL];
651+
CGFloat baseline = lineFragmentRect.origin.y + [layoutManager locationForGlyphAtIndex:glyphIndex].y;
652+
653+
CGRect frame = {
654+
.origin = {glyphRect.origin.x, baseline - attachmentSize.height}, .size = attachmentSize};
654655

655656
auto rect = facebook::react::Rect{
656657
.origin = facebook::react::Point{.x = frame.origin.x, .y = frame.origin.y},

0 commit comments

Comments
 (0)