Skip to content

Commit

Permalink
fix: apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
matinzd committed Jun 17, 2024
1 parent df58587 commit 424323b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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;
Expand Down Expand Up @@ -115,7 +117,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;
Expand Down Expand Up @@ -528,7 +529,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 <a
* TextView#isMultilineInputType(int)}} Source: {@Link <a
* href='https://android.googlesource.com/platform/frameworks/base/+/jb-release/core/java/android/widget/TextView.java'>TextView.java</a>}
*/
if (isMultiline()) {
Expand Down Expand Up @@ -558,18 +559,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) {
Expand Down Expand Up @@ -611,6 +600,7 @@ public void maybeUpdateTypeface() {
if (mFontStyle != ReactConstants.UNSET
|| mFontWeight != ReactConstants.UNSET
|| mFontFamily != null
|| getFontVariationSettingsInternal() != null
|| getFontFeatureSettings() != null) {
setPaintFlags(getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
} else {
Expand Down Expand Up @@ -784,11 +774,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 <T> void stripSpansOfKind(
SpannableStringBuilder sb, Class<T> clazz, Predicate<T> shouldStrip) {
T[] spans = sb.getSpans(0, sb.length(), clazz);
Expand Down Expand Up @@ -843,14 +842,15 @@ private void addSpansFromStyleAttributes(SpannableStringBuilder workingText) {
if (mFontStyle != ReactConstants.UNSET
|| mFontWeight != ReactConstants.UNSET
|| mFontFamily != null
|| getFontVariationSettingsInternal() != null
|| getFontFeatureSettings() != null) {
workingText.setSpan(
new CustomStyleSpan(
mFontStyle,
mFontWeight,
getFontFeatureSettings(),
mFontFamily,
mFontVariationSettings,
getFontVariationSettingsInternal(),
getContext().getAssets()),
0,
workingText.length(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,19 @@ const examples: Array<RNTesterModuleExample> = [
return <ToggleDefaultPaddingExample />;
},
},
{
title: 'TextInput with font variation settings',
render: function (): React.Node {
return (
<ExampleTextInput
style={{
fontFamily: 'inter',
fontVariationSettings: "'wght' 800, 'slnt' -10",
}}
/>
);
},
},
];

module.exports = ({
Expand Down

0 comments on commit 424323b

Please sign in to comment.