From afba1de616fca64fceff8ca0a30fe9a605999fed Mon Sep 17 00:00:00 2001
From: matinzd <24797481+matinzd@users.noreply.github.com>
Date: Fri, 31 May 2024 15:32:29 +0200
Subject: [PATCH] fix: apply suggestions
---
.../text/internal/span/CustomStyleSpan.kt | 2 +-
.../react/views/textinput/ReactEditText.java | 32 +++++++++----------
.../TextInput/TextInputExample.android.js | 13 ++++++++
3 files changed, 30 insertions(+), 17 deletions(-)
diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/internal/span/CustomStyleSpan.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/internal/span/CustomStyleSpan.kt
index c21038a5aa91dc..738e9ff5bd04d9 100644
--- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/internal/span/CustomStyleSpan.kt
+++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/internal/span/CustomStyleSpan.kt
@@ -79,7 +79,7 @@ public class CustomStyleSpan(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
try {
fontVariationSettings = fontVariationSettingsParam
- } catch (e: Exception) {
+ } catch (e: IllegalArgumentException) {
// Do nothing
}
}
diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java
index 853fb5d18c75ce..c517434e6e99aa 100644
--- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java
+++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java
@@ -38,6 +38,8 @@
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;
+import android.widget.TextView;
+
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.core.util.Predicate;
@@ -114,7 +116,6 @@ public class ReactEditText extends AppCompatEditText {
private TextAttributes mTextAttributes;
private boolean mTypefaceDirty = false;
private @Nullable String mFontFamily = null;
- private @Nullable String mFontVariationSettings = null;
private int mFontWeight = ReactConstants.UNSET;
private int mFontStyle = ReactConstants.UNSET;
private boolean mAutoFocus = false;
@@ -527,7 +528,7 @@ public void setInputType(int type) {
/**
* If set forces multiline on input, because of a restriction on Android source that enables
* multiline only for inputs of type Text and Multiline on method {@link
- * android.widget.TextView#isMultilineInputType(int)}} Source: {@Link TextView.java}
*/
if (isMultiline()) {
@@ -557,18 +558,6 @@ public void setFontFamily(String fontFamily) {
mTypefaceDirty = true;
}
- @Override
- public boolean setFontVariationSettings(@Nullable String fontVariationSettings) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- if (!Objects.equals(fontVariationSettings, getFontVariationSettings())) {
- mTypefaceDirty = true;
- mFontVariationSettings = fontVariationSettings;
- return super.setFontVariationSettings(fontVariationSettings);
- }
- }
- return false;
- }
-
public void setFontWeight(String fontWeightString) {
int fontWeight = ReactTypefaceUtils.parseFontWeight(fontWeightString);
if (fontWeight != mFontWeight) {
@@ -610,6 +599,7 @@ public void maybeUpdateTypeface() {
if (mFontStyle != ReactConstants.UNSET
|| mFontWeight != ReactConstants.UNSET
|| mFontFamily != null
+ || getFontVariationSettingsInternal() != null
|| getFontFeatureSettings() != null) {
setPaintFlags(getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
} else {
@@ -783,11 +773,20 @@ private void stripStyleEquivalentSpans(SpannableStringBuilder sb) {
return span.getStyle() == mFontStyle
&& Objects.equals(span.getFontFamily(), mFontFamily)
&& span.getWeight() == mFontWeight
- && Objects.equals(span.getFontVariationSettings(), mFontVariationSettings)
+ && Objects.equals(span.getFontVariationSettings(), getFontVariationSettingsInternal())
&& Objects.equals(span.getFontFeatureSettings(), getFontFeatureSettings());
});
}
+ // Font variation settings is only available on API 26+, we return null if not available
+ private @Nullable String getFontVariationSettingsInternal() {
+ String fontVariationSettings = null;
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ fontVariationSettings = super.getFontVariationSettings();
+ }
+ return fontVariationSettings;
+ }
+
private void stripSpansOfKind(
SpannableStringBuilder sb, Class clazz, Predicate shouldStrip) {
T[] spans = sb.getSpans(0, sb.length(), clazz);
@@ -842,6 +841,7 @@ private void addSpansFromStyleAttributes(SpannableStringBuilder workingText) {
if (mFontStyle != ReactConstants.UNSET
|| mFontWeight != ReactConstants.UNSET
|| mFontFamily != null
+ || getFontVariationSettingsInternal() != null
|| getFontFeatureSettings() != null) {
workingText.setSpan(
new CustomStyleSpan(
@@ -849,7 +849,7 @@ private void addSpansFromStyleAttributes(SpannableStringBuilder workingText) {
mFontWeight,
getFontFeatureSettings(),
mFontFamily,
- mFontVariationSettings,
+ getFontVariationSettingsInternal(),
getContext().getAssets()),
0,
workingText.length(),
diff --git a/packages/rn-tester/js/examples/TextInput/TextInputExample.android.js b/packages/rn-tester/js/examples/TextInput/TextInputExample.android.js
index c2c8eb7084a490..6ecbdff7038858 100644
--- a/packages/rn-tester/js/examples/TextInput/TextInputExample.android.js
+++ b/packages/rn-tester/js/examples/TextInput/TextInputExample.android.js
@@ -522,6 +522,19 @@ const examples: Array = [
return ;
},
},
+ {
+ title: 'TextInput with font variation settings',
+ render: function (): React.Node {
+ return (
+
+ );
+ },
+ },
];
module.exports = ({