Skip to content

Commit 2827a45

Browse files
jasozhfacebook-github-bot
authored andcommitted
Position iOS inline views from their line's actual baseline
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 positions an inline view's frame on the baseline using `glyphRect.origin.y + glyphRect.size.height` to calculate the line bottom, then adding `font.descender` to move up from the line's bottom to its baseline. However, when a line only contains an attachment, no space is reserved below the baseline for descenders (the tails of g, y, p). This means inline views are placed directly at the line bottom instead of the 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 Differential Revision: D127932169
1 parent 1e9dc77 commit 2827a45

1 file changed

Lines changed: 8 additions & 10 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: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -653,16 +653,14 @@ - (TextMeasurement)_measureTextStorage:(NSTextStorage *)textStorage
653653
} else {
654654
CGSize attachmentSize = attachment.bounds.size;
655655
CGRect glyphRect = [layoutManager boundingRectForGlyphRange:range inTextContainer:textContainer];
656-
657-
CGRect frame;
658-
UIFont *font = [[textStorage attributedSubstringFromRange:range] attribute:NSFontAttributeName
659-
atIndex:0
660-
effectiveRange:nil];
661-
frame = {
662-
.origin =
663-
{glyphRect.origin.x,
664-
glyphRect.origin.y + glyphRect.size.height - attachmentSize.height + font.descender},
665-
.size = attachmentSize};
656+
// The attachment sits on the baseline of its line. A line holding only an attachment has no
657+
// descent, so the baseline can't be derived from the font's descender.
658+
CGRect lineFragmentRect = [layoutManager lineFragmentRectForGlyphAtIndex:attachmentGlyphRange.location
659+
effectiveRange:nil];
660+
CGFloat baseline = lineFragmentRect.origin.y +
661+
[layoutManager locationForGlyphAtIndex:attachmentGlyphRange.location].y;
662+
CGRect frame = {
663+
.origin = {glyphRect.origin.x, baseline - attachmentSize.height}, .size = attachmentSize};
666664

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

0 commit comments

Comments
 (0)