From 24b2a66145006886140273264dc198a562fb5278 Mon Sep 17 00:00:00 2001 From: Oleksandr Melnykov Date: Thu, 4 Jul 2019 02:59:31 -0700 Subject: [PATCH] Use HYPHENATION_FREQUENCY_NONE instead of HYPHENATION_FREQUENCY_NORMAL to measure text Summary: [Android] [Fixed] - Use HYPHENATION_FREQUENCY_NONE instead of HYPHENATION_FREQUENCY_NORMAL to measure text The text must be measured with HYPHENATION_FREQUENCY_NONE instead of HYPHENATION_FREQUENCY_NORMAL, since ReactTextView has hyphenation frequency set to HYPHENATION_FREQUENCY_NONE. These two values must match, otherwise the measured height of text we return from the Yoga measure function might be wrong. Even though the TextView [documentation](https://developer.android.com/reference/android/widget/TextView#setHyphenationFrequency(int)) says that the default hyphenation frequency is HYPHENATION_FREQUENCY_NORMAL before Android Q, it's not true for TextViews instantiated in code (the default value is set from the theme which is missing in case of ReactTextView). See the screenshots below where the text is measured incorrectly which causes the last line to be cut off. I extracted the value to a class member variable because I'm planning to expose the hyphenationFrequency prop for the Text component so that it can be configured on Android (as requested by this Github issue: https://github.com/facebook/react-native/issues/17199). Reviewed By: shergin Differential Revision: D16109430 fbshipit-source-id: 278c8182c0f819be27bc1d2468559b9e9ae1f807 --- .../facebook/react/views/text/ReactBaseTextShadowNode.java | 2 ++ .../com/facebook/react/views/text/ReactTextShadowNode.java | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java index f7470b55204..8b9c541ae04 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java @@ -328,6 +328,8 @@ public abstract class ReactBaseTextShadowNode extends LayoutShadowNode { protected int mTextAlign = Gravity.NO_GRAVITY; protected int mTextBreakStrategy = (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) ? 0 : Layout.BREAK_STRATEGY_HIGH_QUALITY; + protected int mHyphenationFrequency = + (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) ? 0 : Layout.HYPHENATION_FREQUENCY_NONE; protected int mJustificationMode = (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) ? 0 : Layout.JUSTIFICATION_MODE_NONE; protected TextTransform mTextTransform = TextTransform.UNSET; diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java index 8e7939b6243..3467aebdc0c 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextShadowNode.java @@ -108,7 +108,7 @@ public class ReactTextShadowNode extends ReactBaseTextShadowNode { .setLineSpacing(0.f, 1.f) .setIncludePad(mIncludeFontPadding) .setBreakStrategy(mTextBreakStrategy) - .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL); + .setHyphenationFrequency(mHyphenationFrequency); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { builder.setJustificationMode(mJustificationMode); @@ -146,7 +146,7 @@ public class ReactTextShadowNode extends ReactBaseTextShadowNode { .setLineSpacing(0.f, 1.f) .setIncludePad(mIncludeFontPadding) .setBreakStrategy(mTextBreakStrategy) - .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL); + .setHyphenationFrequency(mHyphenationFrequency); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { builder.setUseLineSpacingFromFallbacks(true);