Skip to content

Commit 5c062e6

Browse files
authored
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 Pull Request resolved: #59921
1 parent c2aa91a commit 5c062e6

1 file changed

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

705-
CGRect frame;
706-
UIFont *font = [[textStorage attributedSubstringFromRange:range] attribute:NSFontAttributeName
707-
atIndex:0
708-
effectiveRange:nil];
709-
frame = {
710-
.origin =
711-
{glyphRect.origin.x,
712-
glyphRect.origin.y + glyphRect.size.height - attachmentSize.height + font.descender},
713-
.size = attachmentSize};
705+
CGRect lineFragmentRect = [layoutManager lineFragmentRectForGlyphAtIndex:attachmentGlyphRange.location
706+
effectiveRange:nil];
707+
CGFloat baseline = lineFragmentRect.origin.y +
708+
[layoutManager locationForGlyphAtIndex:attachmentGlyphRange.location].y;
709+
CGRect frame = {
710+
.origin = {glyphRect.origin.x, baseline - attachmentSize.height}, .size = attachmentSize};
714711

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

0 commit comments

Comments
 (0)