From 8e5f31edf9ea1e5aca574f3a5e10c453c92708d9 Mon Sep 17 00:00:00 2001 From: kayahickindev <192762269+kayahickindev@users.noreply.github.com> Date: Fri, 30 Oct 2026 15:24:37 -0400 Subject: [PATCH 1/2] Fix Android adjustsFontSizeToFit layout keeping the unshrunk font metrics createLayout measured BoringLayout metrics before adjustSpannableFontToFit shrank the spans and paint, then built the final layout with those metrics. A single-line label shrunk to fit a height got the line height and baseline of its original font size. Co-Authored-By: Claude Opus 5.5 --- .../react/views/text/TextLayoutManager.kt | 12 +- ...utManagerAdjustFontSizeToFitMetricsTest.kt | 116 ++++++++++++++++++ 2 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerAdjustFontSizeToFitMetricsTest.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.kt index d906e9d78e6b..e8682963a08f 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.kt @@ -1217,7 +1217,8 @@ internal object TextLayoutManager { .layout } - private fun createLayout( + @VisibleForTesting + internal fun createLayout( text: Spannable, paint: TextPaint, attributedString: MapBuffer, @@ -1227,7 +1228,7 @@ internal object TextLayoutManager { height: Float, heightYogaMeasureMode: YogaMeasureMode, ): CreateLayoutResult { - val boring = isBoring(text, paint) + var boring = isBoring(text, paint) val textBreakStrategy = TextAttributeProps.getTextBreakStrategy( @@ -1301,6 +1302,11 @@ internal object TextLayoutManager { justificationMode, paint, ) + // The spans and paint now hold the fitted font size. Re-measure so the final layout does not + // keep the line metrics of the original, larger font. + if (boring != null) { + boring = isBoring(text, paint) + } } var layout = @@ -1957,7 +1963,7 @@ internal object TextLayoutManager { return metrics } - private class CreateLayoutResult( + internal class CreateLayoutResult( val layout: Layout, val textBreakStrategy: Int, val justificationMode: Int, diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerAdjustFontSizeToFitMetricsTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerAdjustFontSizeToFitMetricsTest.kt new file mode 100644 index 000000000000..ff2cd22d75ba --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/TextLayoutManagerAdjustFontSizeToFitMetricsTest.kt @@ -0,0 +1,116 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.views.text + +import android.text.Layout +import android.text.SpannableString +import android.text.Spanned +import android.text.TextPaint +import com.facebook.react.common.mapbuffer.WritableMapBuffer +import com.facebook.react.uimanager.DisplayMetricsHolder +import com.facebook.react.views.text.internal.span.ReactAbsoluteSizeSpan +import com.facebook.yoga.YogaMeasureMode +import org.assertj.core.api.Assertions.assertThat +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.GraphicsMode + +/** + * A single-line label that fits its width but not its height is shrunk by adjustsFontSizeToFit. The + * layout createLayout returns must use the line metrics of the fitted font size, not those of the + * original, larger font: otherwise the line is taller than the height it was shrunk to fit, and its + * baseline sits low. PreparedLayoutTextView draws this layout directly, so the label is drawn + * off-center. + */ +@RunWith(RobolectricTestRunner::class) +@GraphicsMode(GraphicsMode.Mode.NATIVE) +class TextLayoutManagerAdjustFontSizeToFitMetricsTest { + + @Before + fun setUp() { + DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(RuntimeEnvironment.getApplication()) + } + + @After + fun tearDown() { + DisplayMetricsHolder.setScreenDisplayMetrics(null) + } + + @Test + fun `text shrunk to fit a height is laid out with the fitted font metrics`() { + val fullSizeLineHeight = layoutWithoutAdjusting().height + val availableHeight = fullSizeLineHeight * 3f / 4f + + val text = spannableWithFontSize(FONT_SIZE) + val paint = newPaint() + val layout = createAdjustedLayout(text, paint, availableHeight) + + assertThat(largestFontSize(text)).isLessThan(FONT_SIZE) + assertThat(layout.height.toFloat()).isLessThanOrEqualTo(availableHeight) + assertThat(layout.getLineBaseline(0)) + .isEqualTo(layoutWithoutAdjusting(text, paint).getLineBaseline(0)) + } + + private fun createAdjustedLayout(text: SpannableString, paint: TextPaint, height: Float): Layout { + return TextLayoutManager.createLayout( + text, + paint, + WritableMapBuffer(), + singleLineParagraph().put(TextLayoutManager.PA_KEY_ADJUST_FONT_SIZE_TO_FIT, true), + WIDTH, + YogaMeasureMode.EXACTLY, + height, + YogaMeasureMode.AT_MOST, + ) + .layout + } + + // The same text at whatever size its spans and paint hold, without adjustsFontSizeToFit. + private fun layoutWithoutAdjusting( + text: SpannableString = spannableWithFontSize(FONT_SIZE), + paint: TextPaint = newPaint(), + ): Layout = + TextLayoutManager.createLayout( + SpannableString(text), + TextPaint(paint), + WritableMapBuffer(), + singleLineParagraph(), + WIDTH, + YogaMeasureMode.EXACTLY, + Float.NaN, + YogaMeasureMode.UNDEFINED, + ) + .layout + + private fun singleLineParagraph(): WritableMapBuffer = + WritableMapBuffer() + .put(TextLayoutManager.PA_KEY_MAX_NUMBER_OF_LINES, 1) + .put(TextLayoutManager.PA_KEY_TEXT_BREAK_STRATEGY, "simple") + .put(TextLayoutManager.PA_KEY_HYPHENATION_FREQUENCY, "none") + + private fun spannableWithFontSize(fontSize: Int): SpannableString { + val text = SpannableString("Done") + text.setSpan(ReactAbsoluteSizeSpan(fontSize), 0, text.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) + return text + } + + private fun newPaint(): TextPaint = + TextPaint(TextPaint.ANTI_ALIAS_FLAG).apply { textSize = FONT_SIZE.toFloat() } + + private fun largestFontSize(text: Spanned): Int = + text.getSpans(0, text.length, ReactAbsoluteSizeSpan::class.java).maxOfOrNull { it.size } ?: 0 + + private companion object { + const val FONT_SIZE = 40 + const val WIDTH = 10_000f + } +} From 8e8f77a46e9fcb0100e9909be25536d0649b3e53 Mon Sep 17 00:00:00 2001 From: kayahickindev <192762269+kayahickindev@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:58:19 -0500 Subject: [PATCH 2/2] Add an RNTester example for adjustsFontSizeToFit shrinking to fit a height Three single-line labels that fit their container's width at full font size but are too tall for its 40dp height. Each should be fully visible and vertically centered once adjustsFontSizeToFit shrinks it. Co-Authored-By: Claude Fable 5.2 --- .../js/examples/Text/TextExample.android.js | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/packages/rn-tester/js/examples/Text/TextExample.android.js b/packages/rn-tester/js/examples/Text/TextExample.android.js index aae2faacf9a0..3f06529714a8 100644 --- a/packages/rn-tester/js/examples/Text/TextExample.android.js +++ b/packages/rn-tester/js/examples/Text/TextExample.android.js @@ -227,6 +227,26 @@ class AdjustingFontSize extends React.Component< } } +function AdjustsFontSizeToFitHeightExample(props: {}): React.Node { + // Each label fits its container's width at its full font size but is too + // tall for it, so adjustsFontSizeToFit shrinks it to fit the height. Every + // label should be fully visible and vertically centered in its box. + return ( + + {[40, 48, 64].map(fontSize => ( + + + Done + + + ))} + + ); +} + function TextLinkifyExample(props: {}): React.Node { return ( <> @@ -1472,6 +1492,11 @@ const examples = [ return ; }, }, + { + title: 'adjustsFontSizeToFit shrinking to fit a height', + name: 'adjustsFontSizeToFitHeight', + render: AdjustsFontSizeToFitHeightExample, + }, { title: 'Wrap', name: 'wrap', @@ -1987,6 +2012,12 @@ const styles = StyleSheet.create({ fontSize: 16, width: 150, }, + adjustsFontSizeToFitHeightBox: { + backgroundColor: '#E4E6EB', + height: 40, + justifyContent: 'center', + marginBottom: 2, + }, }); module.exports = {