From ed26358f63fe27eb0aac3056944811ea69fdea6e Mon Sep 17 00:00:00 2001 From: Joe Vilches Date: Wed, 4 Jun 2025 11:39:15 -0700 Subject: [PATCH] Implement textVerticalAlign (#51680) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/51680 Right now there are 2 flavors of vertical text alignment: `verticalAlign` and `textVerticalAlign`. Both do the same thing currently. For Facsimile, we actually want to "properly" implement `verticalAlign` so that it matches the web version, while leaving `textVerticalAlign` the same. That will take some time, however, so for now we are just going to implement the way it currently works, while fixing some issues with inline View's Changelog: [Internal] Reviewed By: NickGerleman Differential Revision: D75475915 fbshipit-source-id: ba6ff7c831bf769e9e60831ed215ab64faf143a4 --- .../ReactAndroid/api/ReactAndroid.api | 1 + .../react/fabric/FabricUIManager.java | 26 ++--- .../react/views/text/PreparedLayout.kt | 6 +- .../views/text/PreparedLayoutTextView.kt | 3 +- .../react/views/text/TextLayoutManager.java | 103 ++++++++++++++++-- 5 files changed, 108 insertions(+), 31 deletions(-) diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index dfc89c6631a..a92c6de028f 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -6496,6 +6496,7 @@ public class com/facebook/react/views/text/TextLayoutManager { public static final field PA_KEY_MAXIMUM_FONT_SIZE S public static final field PA_KEY_MAX_NUMBER_OF_LINES S public static final field PA_KEY_MINIMUM_FONT_SIZE S + public static final field PA_KEY_TEXT_ALIGN_VERTICAL S public static final field PA_KEY_TEXT_BREAK_STRATEGY S public fun ()V public static fun deleteCachedSpannableForTag (I)V diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java index 7cb6a9d62d6..0b79dd68168 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java @@ -22,7 +22,6 @@ import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Point; import android.os.SystemClock; -import android.text.Layout; import android.view.View; import android.view.accessibility.AccessibilityEvent; import androidx.annotation.AnyThread; @@ -51,7 +50,6 @@ import com.facebook.react.bridge.UIManager; import com.facebook.react.bridge.UIManagerListener; import com.facebook.react.bridge.UiThreadUtil; import com.facebook.react.bridge.WritableMap; -import com.facebook.react.common.ReactConstants; import com.facebook.react.common.annotations.UnstableReactNativeAPI; import com.facebook.react.common.build.ReactBuildConfig; import com.facebook.react.common.mapbuffer.ReadableMapBuffer; @@ -671,23 +669,15 @@ public class FabricUIManager float maxHeight) { SurfaceMountingManager surfaceMountingManager = mMountingManager.getSurfaceManagerEnforced(surfaceId, "prepareLayout"); - Layout layout = - TextLayoutManager.createLayout( - Preconditions.checkNotNull(surfaceMountingManager.getContext()), - attributedString, - paragraphAttributes, - getYogaSize(minWidth, maxWidth), - getYogaMeasureMode(minWidth, maxWidth), - getYogaSize(minHeight, maxHeight), - getYogaMeasureMode(minHeight, maxHeight), - null /* T219881133: Migrate away from ReactTextViewManagerCallback */); - int maximumNumberOfLines = - paragraphAttributes.contains(TextLayoutManager.PA_KEY_MAX_NUMBER_OF_LINES) - ? paragraphAttributes.getInt(TextLayoutManager.PA_KEY_MAX_NUMBER_OF_LINES) - : ReactConstants.UNSET; - - return new PreparedLayout(layout, maximumNumberOfLines); + return TextLayoutManager.createPreparedLayout( + Preconditions.checkNotNull(surfaceMountingManager.getContext()), + attributedString, + paragraphAttributes, + getYogaSize(minWidth, maxWidth), + getYogaMeasureMode(minWidth, maxWidth), + getYogaSize(minHeight, maxHeight), + getYogaMeasureMode(minHeight, maxHeight)); } @AnyThread diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayout.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayout.kt index 92675a02c28..a17b6f04536 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayout.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayout.kt @@ -15,4 +15,8 @@ import com.facebook.proguard.annotations.DoNotStrip * it. */ @DoNotStrip -internal class PreparedLayout(public val layout: Layout, public val maximumNumberOfLines: Int) +internal class PreparedLayout( + public val layout: Layout, + public val maximumNumberOfLines: Int, + public val verticalOffset: Float +) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt index cfa35a9fb60..545e958b223 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/PreparedLayoutTextView.kt @@ -100,7 +100,8 @@ internal class PreparedLayoutTextView(context: Context) : ViewGroup(context), Re } super.onDraw(canvas) - canvas.translate(paddingLeft.toFloat(), paddingTop.toFloat()) + canvas.translate( + paddingLeft.toFloat(), paddingTop.toFloat() + (preparedLayout?.verticalOffset ?: 0f)) val layout = preparedLayout?.layout if (layout != null) { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java index 87bce46bdbd..44b49130cee 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java @@ -26,6 +26,7 @@ import android.view.View; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.util.Preconditions; +import com.facebook.common.logging.FLog; import com.facebook.infer.annotation.Assertions; import com.facebook.react.bridge.ReactNoCrashSoftException; import com.facebook.react.bridge.ReactSoftExceptionLogger; @@ -33,6 +34,7 @@ import com.facebook.react.bridge.WritableArray; import com.facebook.react.common.ReactConstants; import com.facebook.react.common.annotations.UnstableReactNativeAPI; import com.facebook.react.common.mapbuffer.MapBuffer; +import com.facebook.react.common.mapbuffer.ReadableMapBuffer; import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags; import com.facebook.react.uimanager.PixelUtil; import com.facebook.react.uimanager.ReactAccessibilityDelegate.AccessibilityRole; @@ -85,6 +87,7 @@ public class TextLayoutManager { public static final short PA_KEY_HYPHENATION_FREQUENCY = 5; public static final short PA_KEY_MINIMUM_FONT_SIZE = 6; public static final short PA_KEY_MAXIMUM_FONT_SIZE = 7; + public static final short PA_KEY_TEXT_ALIGN_VERTICAL = 8; private static final String TAG = TextLayoutManager.class.getSimpleName(); @@ -604,8 +607,7 @@ public class TextLayoutManager { } } - @UnstableReactNativeAPI - public static Layout createLayout( + private static Layout createLayout( @NonNull Context context, MapBuffer attributedString, MapBuffer paragraphAttributes, @@ -695,6 +697,38 @@ public class TextLayoutManager { paint); } + @UnstableReactNativeAPI + public static PreparedLayout createPreparedLayout( + @NonNull Context context, + ReadableMapBuffer attributedString, + ReadableMapBuffer paragraphAttributes, + float width, + YogaMeasureMode widthYogaMeasureMode, + float height, + YogaMeasureMode heightYogaMeasureMode) { + Layout layout = + TextLayoutManager.createLayout( + Preconditions.checkNotNull(context), + attributedString, + paragraphAttributes, + width, + widthYogaMeasureMode, + height, + heightYogaMeasureMode, + null /* T219881133: Migrate away from ReactTextViewManagerCallback */); + + int maximumNumberOfLines = + paragraphAttributes.contains(TextLayoutManager.PA_KEY_MAX_NUMBER_OF_LINES) + ? paragraphAttributes.getInt(TextLayoutManager.PA_KEY_MAX_NUMBER_OF_LINES) + : ReactConstants.UNSET; + + float verticalOffset = + getVerticalOffset( + layout, paragraphAttributes, height, heightYogaMeasureMode, maximumNumberOfLines); + + return new PreparedLayout(layout, maximumNumberOfLines, verticalOffset); + } + /*package*/ static void adjustSpannableFontToFit( Spannable text, float width, @@ -814,7 +848,7 @@ public class TextLayoutManager { float calculatedWidth = calculateWidth(layout, text, width, widthYogaMeasureMode, calculatedLineCount); float calculatedHeight = - calculateHeight(layout, text, height, heightYogaMeasureMode, calculatedLineCount); + calculateHeight(layout, height, heightYogaMeasureMode, calculatedLineCount); if (attachmentsPositions != null) { int attachmentIndex = 0; @@ -823,7 +857,8 @@ public class TextLayoutManager { AttachmentMetrics metrics = new AttachmentMetrics(); for (int i = 0; i < text.length(); i = lastAttachmentFoundInSpan) { lastAttachmentFoundInSpan = - nextAttachmentMetrics(layout, text, calculatedWidth, calculatedLineCount, i, metrics); + nextAttachmentMetrics( + layout, text, calculatedWidth, calculatedLineCount, i, 0, metrics); if (metrics.wasFound) { attachmentsPositions[attachmentIndex] = PixelUtil.toDIPFromPixel(metrics.top); attachmentsPositions[attachmentIndex + 1] = PixelUtil.toDIPFromPixel(metrics.left); @@ -853,7 +888,7 @@ public class TextLayoutManager { float calculatedWidth = calculateWidth(layout, text, width, widthYogaMeasureMode, calculatedLineCount); float calculatedHeight = - calculateHeight(layout, text, height, heightYogaMeasureMode, calculatedLineCount); + calculateHeight(layout, height, heightYogaMeasureMode, calculatedLineCount); ArrayList retList = new ArrayList<>(); retList.add(PixelUtil.toDIPFromPixel(calculatedWidth)); @@ -863,7 +898,14 @@ public class TextLayoutManager { int lastAttachmentFoundInSpan; for (int i = 0; i < text.length(); i = lastAttachmentFoundInSpan) { lastAttachmentFoundInSpan = - nextAttachmentMetrics(layout, text, calculatedWidth, calculatedLineCount, i, metrics); + nextAttachmentMetrics( + layout, + text, + calculatedWidth, + calculatedLineCount, + i, + preparedLayout.getVerticalOffset(), + metrics); if (metrics.wasFound) { retList.add(PixelUtil.toDIPFromPixel(metrics.top)); retList.add(PixelUtil.toDIPFromPixel(metrics.left)); @@ -879,6 +921,44 @@ public class TextLayoutManager { return ret; } + private static float getVerticalOffset( + Layout layout, + ReadableMapBuffer paragraphAttributes, + float height, + YogaMeasureMode heightMeasureMode, + int maximumNumberOfLines) { + @Nullable + String textAlignVertical = + paragraphAttributes.contains(TextLayoutManager.PA_KEY_TEXT_ALIGN_VERTICAL) + ? paragraphAttributes.getString(TextLayoutManager.PA_KEY_TEXT_ALIGN_VERTICAL) + : null; + + if (textAlignVertical == null) { + return 0; + } + + int textHeight = layout.getHeight(); + int calculatedLineCount = calculateLineCount(layout, maximumNumberOfLines); + float boxHeight = calculateHeight(layout, height, heightMeasureMode, calculatedLineCount); + + if (textHeight > boxHeight) { + return 0; + } + + switch (textAlignVertical) { + case "auto": + case "top": + return 0; + case "center": + return (boxHeight - textHeight) / 2f; + case "bottom": + return boxHeight - textHeight; + default: + FLog.w(ReactConstants.TAG, "Invalid textAlignVertical: " + textAlignVertical); + return 0; + } + } + private static int calculateLineCount(Layout layout, int maximumNumberOfLines) { return maximumNumberOfLines == ReactConstants.UNSET || maximumNumberOfLines == 0 ? layout.getLineCount() @@ -945,11 +1025,7 @@ public class TextLayoutManager { } private static float calculateHeight( - Layout layout, - Spanned text, - float height, - YogaMeasureMode heightYogaMeasureMode, - int calculatedLineCount) { + Layout layout, float height, YogaMeasureMode heightYogaMeasureMode, int calculatedLineCount) { float calculatedHeight = height; if (heightYogaMeasureMode != YogaMeasureMode.EXACTLY) { // StaticLayout only seems to change its height in response to maxLines when ellipsizing, so @@ -976,6 +1052,7 @@ public class TextLayoutManager { float calculatedWidth, int calculatedLineCount, int i, + float verticalOffset, AttachmentMetrics metrics) { // Calculate the positions of the attachments (views) that will be rendered inside the // Spanned Text. The following logic is only executed when a text contains views inside. @@ -1062,6 +1139,10 @@ public class TextLayoutManager { metrics.left = placeholderLeftPosition; } + // The text may be vertically aligned to the top, center, or bottom of the container. This is + // not captured in the Layout, but rather applied separately. We need to account for this here. + metrics.top += verticalOffset; + metrics.wasFound = true; metrics.width = placeholder.getWidth(); metrics.height = placeholder.getHeight();