From f0a51cbf1153ba2cc131255380f7ee7fb86a937e Mon Sep 17 00:00:00 2001 From: Jason Zheng Date: Thu, 19 Nov 2026 14:12:47 -0800 Subject: [PATCH] Position iOS inline views from their line's actual baseline (#59921) Summary: Fixes an issue where inline views in text on iOS are not properly positioned from their actual baseline, resulting in situations where the view/image appears lower than expected or with less padding from the previous line. ### Root Cause After TextKit lays out the text, RCTTextLayoutManager tries to position an inline view's frame on the baseline. It does so by using `glyphRect.origin.y + glyphRect.size.height` to calculate the line bottom, then adding `font.descender` to derive the baseline. However, this fails in two cases. First, when a line only contains an attachment, the baseline should be the line bottom, but the current formula still adds `font.descender` which positions the view too high and clips into the previous line. Second, when `lineHeight` is set, text is lifted by an `NSBaselineOffset` to center it. The current formula fails to account for this offset, so inline views appear placed below the actual text baseline. ### This PR Modify RCTTextLayoutManager so that instead of manually computing the baseline, we ask TextKit for the baseline used on that line. Changelog: [iOS][Fixed] - Fix inline views on iOS being misaligned from the baseline Reviewed By: vzaidman Differential Revision: D127932169 --- .../textlayoutmanager/RCTTextLayoutManager.mm | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm index 0d9100187bd2..83e63854cd14 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm @@ -702,15 +702,12 @@ - (TextMeasurement)_measureTextStorage:(NSTextStorage *)textStorage CGSize attachmentSize = attachment.bounds.size; CGRect glyphRect = [layoutManager boundingRectForGlyphRange:range inTextContainer:textContainer]; - CGRect frame; - UIFont *font = [[textStorage attributedSubstringFromRange:range] attribute:NSFontAttributeName - atIndex:0 - effectiveRange:nil]; - frame = { - .origin = - {glyphRect.origin.x, - glyphRect.origin.y + glyphRect.size.height - attachmentSize.height + font.descender}, - .size = attachmentSize}; + CGRect lineFragmentRect = [layoutManager lineFragmentRectForGlyphAtIndex:attachmentGlyphRange.location + effectiveRange:nil]; + CGFloat baseline = lineFragmentRect.origin.y + + [layoutManager locationForGlyphAtIndex:attachmentGlyphRange.location].y; + CGRect frame = { + .origin = {glyphRect.origin.x, baseline - attachmentSize.height}, .size = attachmentSize}; auto rect = facebook::react::Rect{ .origin = facebook::react::Point{.x = frame.origin.x, .y = frame.origin.y},