diff --git a/Libraries/Text/Text.js b/Libraries/Text/Text.js index 660dd87976121f..6ff8c14c267d92 100644 --- a/Libraries/Text/Text.js +++ b/Libraries/Text/Text.js @@ -68,6 +68,7 @@ const viewConfig = { onTextLayout: true, onInlineViewLayout: true, dataDetectorType: true, + android_hyphenationFrequency: true, }, directEventTypes: { topTextLayout: { diff --git a/Libraries/Text/TextProps.js b/Libraries/Text/TextProps.js index 009929e8ff7076..4b48bc57066a4d 100644 --- a/Libraries/Text/TextProps.js +++ b/Libraries/Text/TextProps.js @@ -57,6 +57,18 @@ export type TextProps = $ReadOnly<{| * See https://reactnative.dev/docs/text.html#allowfontscaling */ allowFontScaling?: ?boolean, + + /** + * Set hyphenation strategy on Android. + * + */ + android_hyphenationFrequency?: ?( + | 'normal' + | 'none' + | 'full' + | 'high' + | 'balanced' + ), children?: ?Node, /** diff --git a/RNTester/js/examples/Text/TextExample.android.js b/RNTester/js/examples/Text/TextExample.android.js index f9631821f9900f..cec4b348d19fe4 100644 --- a/RNTester/js/examples/Text/TextExample.android.js +++ b/RNTester/js/examples/Text/TextExample.android.js @@ -146,6 +146,7 @@ class AdjustingFontSize extends React.Component< {'Multiline text component shrinking is supported, watch as this reeeeaaaally loooooong teeeeeeext grooooows and then shriiiinks as you add text to me! ioahsdia soady auydoa aoisyd aosdy ' + ' ' + @@ -207,6 +208,28 @@ class TextExample extends React.Component<{...}> { going to the next line. + + + Normal: + WillHaveAnHyphenWhenBreakingForNewLine + + + None: + WillNotHaveAnHyphenWhenBreakingForNewLine + + + Full: + WillHaveAnHyphenWhenBreakingForNewLine + + + High: + WillHaveAnHyphenWhenBreakingForNewLine + + + Balanced: + WillHaveAnHyphenWhenBreakingForNewLine + + This text is indented by 10px padding on all sides. diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextAnchorViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextAnchorViewManager.java index 401c8a0fa34a74..4a448e9174704d 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextAnchorViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextAnchorViewManager.java @@ -7,6 +7,8 @@ package com.facebook.react.views.text; +import android.os.Build; +import android.text.Layout; import android.text.Spannable; import android.text.TextUtils; import android.text.util.Linkify; @@ -96,6 +98,26 @@ public void setSelectionColor(ReactTextView view, @Nullable Integer color) { } } + @ReactProp(name = "android_hyphenationFrequency") + public void setAndroidHyphenationFrequency(ReactTextView view, @Nullable String frequency) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { + return; + } + if (frequency == null || frequency.equals("none")) { + view.setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE); + } else if (frequency.equals("full")) { + view.setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_FULL); + } else if (frequency.equals("balanced")) { + view.setHyphenationFrequency(Layout.BREAK_STRATEGY_BALANCED); + } else if (frequency.equals("high")) { + view.setHyphenationFrequency(Layout.BREAK_STRATEGY_HIGH_QUALITY); + } else if (frequency.equals("normal")) { + view.setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL); + } else { + throw new JSApplicationIllegalArgumentException("Invalid android_hyphenationFrequency: " + frequency); + } + } + @ReactPropGroup( names = { ViewProps.BORDER_RADIUS,