mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Fix Android inline Text views with small font scale
Summary: Fixes https://github.com/facebook/react-native/issues/50916. Closes torin-asakura/workspace#126. Android inline views inside `<Text>` receive their measured layout size from Fabric as layout units/DIP. TextLayoutManager was converting those attachment dimensions with `PixelUtil.toPixelFromSP(...)`, so a device font scale below 1.0 shrank the inline view placeholder width. In the https://github.com/facebook/react-native/issues/50916 RNTester reproducer, a measured `Row cutoff` attachment around 155px became about 132px at `font_scale=0.85`, clipping the rendered text to `Row`. This changes inline text attachment dimensions to use DIP conversion and rounds them up to the pixel grid in both spannable construction paths. ## Changelog: [ANDROID] [FIXED] - Keep inline views inside Text from shrinking with Android system font scale X-link: https://github.com/facebook/react-native/pull/57132 Reviewed By: christophpurrer Differential Revision: D108030457 Pulled By: cipolleschi fbshipit-source-id: af68489aed0ee9c39f3a440f07ca9286ff5ed30c
This commit is contained in:
1 parent
2b995be464
commit
551d12a787
2 files changed
+62
-5
No files matched your search
+11
-5
@@ -266,13 +266,15 @@ internal object TextLayoutManager {
|
||||
val reactTag =
|
||||
if (fragment.contains(FR_KEY_REACT_TAG)) fragment.getInt(FR_KEY_REACT_TAG) else View.NO_ID
|
||||
if (fragment.contains(FR_KEY_IS_ATTACHMENT) && fragment.getBoolean(FR_KEY_IS_ATTACHMENT)) {
|
||||
val width = PixelUtil.toPixelFromSP(fragment.getDouble(FR_KEY_WIDTH))
|
||||
val height = PixelUtil.toPixelFromSP(fragment.getDouble(FR_KEY_HEIGHT))
|
||||
ops.add(
|
||||
SetSpanOperation(
|
||||
sb.length - 1,
|
||||
sb.length,
|
||||
TextInlineViewPlaceholderSpan(reactTag, width.toInt(), height.toInt()),
|
||||
TextInlineViewPlaceholderSpan(
|
||||
reactTag,
|
||||
inlineViewSizeToPixels(fragment.getDouble(FR_KEY_WIDTH)),
|
||||
inlineViewSizeToPixels(fragment.getDouble(FR_KEY_HEIGHT)),
|
||||
),
|
||||
),
|
||||
)
|
||||
} else if (end >= start) {
|
||||
@@ -490,8 +492,8 @@ internal object TextLayoutManager {
|
||||
spannable.setSpan(
|
||||
TextInlineViewPlaceholderSpan(
|
||||
fragment.reactTag,
|
||||
PixelUtil.toPixelFromSP(fragment.width).toInt(),
|
||||
PixelUtil.toPixelFromSP(fragment.height).toInt(),
|
||||
inlineViewSizeToPixels(fragment.width),
|
||||
inlineViewSizeToPixels(fragment.height),
|
||||
),
|
||||
start,
|
||||
end,
|
||||
@@ -658,6 +660,10 @@ internal object TextLayoutManager {
|
||||
return spannable
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
internal fun inlineViewSizeToPixels(size: Double): Int =
|
||||
ceil(PixelUtil.toPixelFromDIP(size).toDouble()).toInt()
|
||||
|
||||
@OptIn(UnstableReactNativeAPI::class)
|
||||
fun getOrCreateSpannableForText(
|
||||
assets: AssetManager,
|
||||
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
@file:Suppress("DEPRECATION")
|
||||
|
||||
package com.facebook.react.views.text
|
||||
|
||||
import android.util.DisplayMetrics
|
||||
import com.facebook.react.uimanager.DisplayMetricsHolder
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.After
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class TextLayoutManagerInlineViewSizeTest {
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
DisplayMetricsHolder.setScreenDisplayMetrics(null)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `inline view attachment width does not shrink with small font scale`() {
|
||||
DisplayMetricsHolder.setScreenDisplayMetrics(
|
||||
DisplayMetrics().apply {
|
||||
density = 1f
|
||||
scaledDensity = 0.85f
|
||||
},
|
||||
)
|
||||
|
||||
assertThat(TextLayoutManager.inlineViewSizeToPixels(155.0)).isEqualTo(155)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `inline view attachment width is rounded up to the pixel grid`() {
|
||||
DisplayMetricsHolder.setScreenDisplayMetrics(
|
||||
DisplayMetrics().apply {
|
||||
density = 1f
|
||||
scaledDensity = 1f
|
||||
},
|
||||
)
|
||||
|
||||
assertThat(TextLayoutManager.inlineViewSizeToPixels(132.1)).isEqualTo(133)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user