diff --git a/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp b/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp index 910d73f39460..f2890633ce2e 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp @@ -401,7 +401,11 @@ Float ParagraphShadowNode::baseline( TextLayoutManagerExtended(*textLayoutManager_) .measureLines( attributedStringBox, content.paragraphAttributes, size); - return LineMeasurement::baseline(lines); + // Yoga expects the baseline from the border box; the ascender is measured + // from the top of the text content. + auto topInset = YGNodeLayoutGetBorder(&yogaNode_, YGEdgeTop) + + YGNodeLayoutGetPadding(&yogaNode_, YGEdgeTop); + return topInset + LineMeasurement::baseline(lines); } else { LOG(WARNING) << "Baseline alignment is not supported by the current platform"; diff --git a/packages/rn-tester/.maestro/text-baseline-insets.yml b/packages/rn-tester/.maestro/text-baseline-insets.yml new file mode 100644 index 000000000000..690eb86a97d5 --- /dev/null +++ b/packages/rn-tester/.maestro/text-baseline-insets.yml @@ -0,0 +1,9 @@ +appId: ${APP_ID} +--- +- launchApp +- stopApp +- openLink: rntester://example/Text/paddedTextBaseline +- runFlow: ./helpers/confirm-open-link.yml +- extendedWaitUntil: + visible: 'Top edge difference: 24 dp' + timeout: 30000 diff --git a/packages/rn-tester/js/examples/Text/TextSharedExamples.js b/packages/rn-tester/js/examples/Text/TextSharedExamples.js index 63e4d837beee..b877ce445b94 100644 --- a/packages/rn-tester/js/examples/Text/TextSharedExamples.js +++ b/packages/rn-tester/js/examples/Text/TextSharedExamples.js @@ -265,7 +265,37 @@ function LastLineClippingExample(): React.Node { ); } +function PaddedTextBaselineExample(): React.Node { + const [leftY, setLeftY] = useState(null); + const [rightY, setRightY] = useState(null); + return ( + + + setLeftY(event.nativeEvent.layout.y)} + style={{paddingBottom: 8}}> + Aa + + setRightY(event.nativeEvent.layout.y)} + style={{paddingTop: 20, borderTopWidth: 4}}> + Aa + + + {leftY != null && rightY != null && ( + Top edge difference: {Math.round(leftY - rightY)} dp + )} + + ); +} + export default [ + { + title: 'Baseline alignment with padding and border', + name: 'paddedTextBaseline', + description: 'The text should line up despite the different top edges.', + render: PaddedTextBaselineExample, + }, { title: 'Empty Text', name: 'emptyText',