mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Fix adjustsFontSizeToFit clipping when container height shrinks on Android (#57597)
Summary: When a `<Text adjustsFontSizeToFit>` component's container height decreases dynamically (e.g. height=60 → height=20), the text is clipped instead of having its font size recalculated. **Root cause:** `mShouldAdjustSpannableFontSize` — the gate that triggers font recalculation in `onDraw` — is only set by text-content setters (`setSpanned`, `setNumberOfLines`, etc.). A pure container resize never sets it. Meanwhile the background Fabric measurement thread mutates the shared cached spannable (`tagToSpannableCache`) to the larger font size. When the container then shrinks, `onDraw` skips recalculation and draws the stale large font in the smaller container, causing clipping. **Fix:** Override `onSizeChanged` in `ReactTextView` to set `mShouldAdjustSpannableFontSize = true` whenever the view dimensions change and `adjustsFontSizeToFit` is active. Android automatically schedules a redraw on size change, so the next `onDraw` recalculates the font with the correct current height. **Note on the shared spannable:** the recalculation in `onDraw` mutates the (possibly shared) cached spannable on the UI thread via `adjustSpannableFontToFit` followed by `setText`. This change makes that recalculation also run on a container resize, so it rides the existing UI-thread mutation of the shared spannable a bit more often. It is the same mutation already performed on content changes, so it does not introduce a new race, but it is worth flagging if the spannable caching is revisited. ## Changelog: [ANDROID] [FIXED] - Fix Text with adjustsFontSizeToFit being clipped when container height decreases dynamically Pull Request resolved: https://github.com/react/react-native/pull/57597 Test Plan: Tested using the existing RNTester example which already reproduces this bug: 1. Build and run RNTester on Android 2. Navigate to **Text** → **Font Size Adjustment with Dynamic Layout** 3. Press **Set Height to 60** — text grows to fill the yellow container 4. Press **Set Height to 20** — before this fix, text is clipped; after this fix, text shrinks to fit correctly 5. Repeat steps 3–4 rapidly — no clipping at any point The reproducer is `packages/rn-tester/js/examples/Text/TextAdjustsDynamicLayoutExample.js`. Automated: added `ReactTextViewTest.adjustsFontSizeToFitRecalculatesWhenContainerHeightShrinks`, which lays out an `adjustsFontSizeToFit` text at a tall height then a short height and asserts the `ReactAbsoluteSizeSpan` font size shrinks. It fails without this change (the resize does not retrigger recalculation) and passes with it. NOTE: Addition video provided by the author in the PR. Reviewed By: cortinico Differential Revision: D112811263 Pulled By: fabriziocucci fbshipit-source-id: 411f61c9f678a3fc36a073300ce3cb57b56e2be6
This commit is contained in:
1 parent
38611186f5
commit
63064687e2
3 files changed
+62
No files matched your search
@@ -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
|
||||
|
||||
+8
@@ -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
|
||||
|
||||
+53
@@ -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)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user