Fix text clipping on Android 15+ due to useBoundsForWidth change (#54721)

Summary:
Android 15 (API 35) changed TextView's default behavior to use visual glyph bounds for width calculation (useBoundsForWidth=true).  This causes text clipping for italic fonts and other typefaces where glyphs extend beyond their advance width:

https://developer.android.com/about/versions/15/behavior-changes-15

This PR uses a two-pass layout approach on Android 15+:

1. Create an unconstrained layout with setUseBoundsForWidth(true) to measure the actual visual bounds width
2. Create the final layout using the visual bounds width as layoutWidth

This ensures the container is sized correctly to accommodate the full visual extent of the text, preventing clipping while maintaining backwards compatibility with earlier Android versions.

Performance impact:
- On Android 14 and below: No change
- On Android 15+: Creates two StaticLayout instances instead of one for text with unconstrained or AT_MOST width.  This overhead is mitigated by React Native's text measurement caching in the C++ layer, which avoids redundant measurements.

Text with EXACTLY width mode is unaffected as it only requires a single layout pass.

Closes https://github.com/facebook/react-native/issues/53286

Thanks a lot to intergalacticspacehighway for finding the root cause and creating a proof of concept fix.

## Changelog:

[ANDROID] [FIXED] - Fix text clipping on Android 15+ due to useBoundsForWidth change

Pull Request resolved: https://github.com/facebook/react-native/pull/54721

Test Plan:
Tested on RNTester + Android 16 on physical device:

|  before | after |
| -- |
|   {F1983971719} | {F1983971720} |

Also tested on the reproducer provided by https://github.com/facebook/react-native/issues/53286

|  before | after |
| -- |
|  {F1983971723} | {F1983971718} |

Reviewed By: javache

Differential Revision: D88001642

Pulled By: cortinico

fbshipit-source-id: 4bc20ef6da47bd4141f6b19fcc3e93ea7f0c343c
This commit is contained in:
Christian Falch authored and meta-codesync[bot] committed 2025-12-07 16:05:08 -08:00
1 parent d176e09427
commit 8347cc4b50
2 files changed
+89 -4

No files matched your search

@@ -620,15 +620,89 @@ internal object TextLayoutManager {
)
}
val desiredWidth = ceil(Layout.getDesiredWidth(text, paint)).toInt()
// Pre-Android 15: Use existing advance-based logic
if (
Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM ||
!ReactNativeFeatureFlags.fixTextClippingAndroid15useBoundsForWidth()
) {
val desiredWidth = ceil(Layout.getDesiredWidth(text, paint)).toInt()
val layoutWidth =
when (widthYogaMeasureMode) {
YogaMeasureMode.EXACTLY -> floor(width).toInt()
YogaMeasureMode.AT_MOST -> min(desiredWidth, floor(width).toInt())
else -> desiredWidth
}
return buildLayout(
text,
layoutWidth,
includeFontPadding,
textBreakStrategy,
hyphenationFrequency,
alignment,
justificationMode,
ellipsizeMode,
maxNumberOfLines,
paint,
)
}
// Android 15+: Need to account for visual bounds
// Step 1: Create unconstrained layout to get visual bounds width
val unconstrainedLayout =
buildLayout(
text,
Int.MAX_VALUE / 2,
includeFontPadding,
textBreakStrategy,
hyphenationFrequency,
alignment,
justificationMode,
null,
ReactConstants.UNSET,
paint,
)
// Calculate visual bounds width from unconstrained layout
var desiredVisualWidth = 0f
for (i in 0 until unconstrainedLayout.lineCount) {
val lineWidth = unconstrainedLayout.getLineRight(i) - unconstrainedLayout.getLineLeft(i)
desiredVisualWidth = max(desiredVisualWidth, lineWidth)
}
val layoutWidth =
when (widthYogaMeasureMode) {
YogaMeasureMode.EXACTLY -> floor(width).toInt()
YogaMeasureMode.AT_MOST -> min(desiredWidth, floor(width).toInt())
else -> desiredWidth
YogaMeasureMode.AT_MOST -> min(ceil(desiredVisualWidth).toInt(), floor(width).toInt())
else -> ceil(desiredVisualWidth).toInt()
}
// Step 2: Create final layout with correct width
return buildLayout(
text,
layoutWidth,
includeFontPadding,
textBreakStrategy,
hyphenationFrequency,
alignment,
justificationMode,
ellipsizeMode,
maxNumberOfLines,
paint,
)
}
private fun buildLayout(
text: Spannable,
layoutWidth: Int,
includeFontPadding: Boolean,
textBreakStrategy: Int,
hyphenationFrequency: Int,
alignment: Layout.Alignment,
justificationMode: Int,
ellipsizeMode: TextUtils.TruncateAt?,
maxNumberOfLines: Int,
paint: TextPaint,
): Layout {
val builder =
StaticLayout.Builder.obtain(text, 0, text.length, paint, layoutWidth)
.setAlignment(alignment)
@@ -649,6 +723,13 @@ internal object TextLayoutManager {
builder.setUseLineSpacingFromFallbacks(true)
}
if (
Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM &&
ReactNativeFeatureFlags.fixTextClippingAndroid15useBoundsForWidth()
) {
builder.setUseBoundsForWidth(true)
}
return builder.build()
}
@@ -1408,6 +1408,10 @@ const examples = [
<RNTesterText style={{fontStyle: 'normal'}}>
Move fast and be normal
</RNTesterText>
<RNTesterText style={{fontStyle: 'italic'}}>
Move fast and be italic, but just be longer so that you don't fit on
a single line and make sure text is not truncated.
</RNTesterText>
</>
);
},