diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 57ed2ec2aa7..cd91a0af37f 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -6029,6 +6029,7 @@ public class com/facebook/react/views/text/ReactTextView : androidx/appcompat/wi public final fun onFocusChanged (ZILandroid/graphics/Rect;)V protected fun onLayout (ZIIII)V protected fun onMeasure (II)V + protected fun onSizeChanged (IIII)V public fun reactTagForTouch (FF)I public fun setAdjustFontSizeToFit (Z)V public fun setBackgroundColor (I)V diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java index 6b6b848385e..6df3bfa86dd 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java @@ -313,6 +313,14 @@ public class ReactTextView extends AppCompatTextView implements ReactCompoundVie } } + @Override + protected void onSizeChanged(int w, int h, int oldw, int oldh) { + super.onSizeChanged(w, h, oldw, oldh); + if (mAdjustsFontSizeToFit && (w != oldw || h != oldh)) { + mShouldAdjustSpannableFontSize = true; + } + } + public void setText(ReactTextUpdate update) { try (SystraceSection s = new SystraceSection("ReactTextView.setText(ReactTextUpdate)")) { // Android's TextView crashes when it tries to relayout if LayoutParams are diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/ReactTextViewTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/ReactTextViewTest.kt index 3cbb6435cf3..fe8d5bf6a55 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/ReactTextViewTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/text/ReactTextViewTest.kt @@ -18,8 +18,10 @@ import android.text.style.ReplacementSpan import android.util.TypedValue import android.view.Gravity import android.view.View +import android.view.ViewGroup import androidx.core.graphics.createBitmap import androidx.core.graphics.get +import com.facebook.react.views.text.internal.span.ReactAbsoluteSizeSpan import org.assertj.core.api.Assertions.assertThat import org.junit.Test import org.junit.runner.RunWith @@ -45,6 +47,57 @@ class ReactTextViewTest { assertThat(firstVisiblePixelY(bitmap)).isGreaterThanOrEqualTo(lineHeight) } + @Test + fun adjustsFontSizeToFitRecalculatesWhenContainerHeightShrinks() { + val width = 200 + val tallHeight = 200 + val shortHeight = 20 + val largeFontSize = 40 + + val text = SpannableString("Hello") + text.setSpan( + ReactAbsoluteSizeSpan(largeFontSize), + 0, + text.length, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE, + ) + + val view = TestReactTextView(RuntimeEnvironment.getApplication()) + view.layoutParams = + ViewGroup.LayoutParams( + ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.WRAP_CONTENT, + ) + view.setTextColor(Color.BLACK) + view.setMinimumFontSize(4f) + view.setNumberOfLines(0) + view.setAdjustFontSizeToFit(true) + view.setSpanned(text) + view.text = text + + layoutAndDraw(view, width, tallHeight) + val fontSizeWhenTall = largestAbsoluteSizeSpan(text) + + // Shrinking the container must retrigger the font recalculation, otherwise the + // stale large font is drawn into the smaller container and gets clipped. + layoutAndDraw(view, width, shortHeight) + val fontSizeWhenShort = largestAbsoluteSizeSpan(text) + + assertThat(fontSizeWhenShort).isLessThan(fontSizeWhenTall) + } + + private fun layoutAndDraw(view: TestReactTextView, width: Int, height: Int) { + view.measure( + View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY), + View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY), + ) + view.layout(0, 0, width, height) + view.drawTextForTest(Canvas(createBitmap(width, height))) + } + + private fun largestAbsoluteSizeSpan(text: Spanned): Int = + text.getSpans(0, text.length, ReactAbsoluteSizeSpan::class.java).maxOfOrNull { it.size } ?: 0 + private fun drawReactTextViewWithOverflow(overflow: String?): Bitmap { return drawReactTextViewWithOverflow(overflow, lineHeight = 24, viewHeight = 24, gravity = null) }