Skip to content

Commit

Permalink
feat(android): fontWeight support number value (#3570)
Browse files Browse the repository at this point in the history
* feat(android): fontWeight support number value

* feat(android): fontWeight support number for `TextInput`

---------

Co-authored-by: OpenHippy <[email protected]>
  • Loading branch information
iPel and open-hippy authored Nov 23, 2023
1 parent e677e0d commit bb9b7db
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 149 deletions.
2 changes: 2 additions & 0 deletions docs/api/style/appearance.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@
| ------ | -------- | --- |
| number \| string | 否 | Android、iOS

> Android API 28 以下仅支持 `normal``400`) 和 `bold``700`)两种字重,其它字重效果需配合 `fontFamily` 实现。
# letterSpacing

文本字符间距
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@
import android.graphics.BlendMode;
import android.graphics.BlendModeColorFilter;

import android.graphics.Paint;
import android.graphics.Typeface;
import androidx.annotation.Nullable;
import com.tencent.mtt.hippy.common.HippyMap;
import com.tencent.mtt.hippy.uimanager.HippyViewBase;
import com.tencent.mtt.hippy.uimanager.NativeGestureDispatcher;
import com.tencent.mtt.hippy.uimanager.RenderManager;
import com.tencent.renderer.NativeRender;
import com.tencent.renderer.NativeRendererManager;
import com.tencent.renderer.component.text.FontAdapter;
import com.tencent.renderer.component.text.TypeFaceUtil;
import com.tencent.renderer.node.RenderNode;
import com.tencent.mtt.hippy.utils.ContextHolder;
import com.tencent.mtt.hippy.utils.LogUtils;
Expand Down Expand Up @@ -57,6 +64,7 @@
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@SuppressWarnings({"deprecation", "unused"})
public class HippyTextInput extends AppCompatEditText implements HippyViewBase,
Expand All @@ -75,6 +83,11 @@ public class HippyTextInput extends AppCompatEditText implements HippyViewBase,
private int mLastRootViewVisibleHeight = -1; //当前RootView的上一次大小
private boolean mIsKeyBoardShow = false; //键盘是否在显示
private ReactContentSizeWatcher mReactContentSizeWatcher = null;
private boolean mItalic = false;
private int mFontWeight = TypeFaceUtil.WEIGHT_NORMAL;
@Nullable
private String mFontFamily;
private Paint mTextPaint;

public HippyTextInput(Context context) {
super(context);
Expand Down Expand Up @@ -639,4 +652,53 @@ public void refreshSoftInput() {
}
}
}

public void setFontStyle(String style) {
if (TypeFaceUtil.TEXT_FONT_STYLE_ITALIC.equals(style) != mItalic) {
mItalic = !mItalic;
updateTypeface();
}
}

public void setFontFamily(String family) {
if (!Objects.equals(mFontFamily, family)) {
mFontFamily = family;
updateTypeface();
}
}

public void setFontWeight(String weight) {
int fontWeight;
if (TextUtils.isEmpty(weight) || TypeFaceUtil.TEXT_FONT_STYLE_NORMAL.equals(weight)) {
// case normal
fontWeight = TypeFaceUtil.WEIGHT_NORMAL;
} else if (TypeFaceUtil.TEXT_FONT_STYLE_BOLD.equals(weight)) {
// case bold
fontWeight = TypeFaceUtil.WEIGHT_BOLE;
} else {
// case number
try {
fontWeight = Math.min(Math.max(1, Integer.parseInt(weight)), 1000);
} catch (NumberFormatException ignored) {
fontWeight = TypeFaceUtil.WEIGHT_NORMAL;
}
}
if (fontWeight != mFontWeight) {
mFontWeight = fontWeight;
updateTypeface();
}
}

private void updateTypeface() {
if (mTextPaint == null) {
mTextPaint = new Paint();
} else {
mTextPaint.reset();
}
NativeRender nativeRenderer = NativeRendererManager.getNativeRenderer(getContext());
FontAdapter fontAdapter = nativeRenderer == null ? null : nativeRenderer.getFontAdapter();
TypeFaceUtil.apply(mTextPaint, mItalic, mFontWeight, mFontFamily, fontAdapter);
setTypeface(mTextPaint.getTypeface(), mTextPaint.isFakeBoldText() ? Typeface.BOLD : Typeface.NORMAL);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -208,66 +208,19 @@ public void setKeyboardType(HippyTextInput hippyTextInput, String keyboardType)
hippyTextInput.refreshSoftInput();
}

private static int parseFontWeight(String fontWeightString) {
// This should be much faster than using regex to verify input and Integer.parseInt
return fontWeightString.length() == 3 && fontWeightString.endsWith("00")
&& fontWeightString.charAt(0) <= '9'
&& fontWeightString.charAt(0) >= '1' ? 100 * (fontWeightString.charAt(0) - '0')
: -1;
}

@HippyControllerProps(name = NodeProps.FONT_STYLE, defaultType = HippyControllerProps.STRING, defaultString = "normal")
@HippyControllerProps(name = NodeProps.FONT_STYLE, defaultType = HippyControllerProps.STRING)
public void setFontStyle(HippyTextInput view, String fontStyleString) {
if (TextUtils.isEmpty(fontStyleString)) {
return;
}
int fontStyle = -1;
if ("italic".equals(fontStyleString)) {
fontStyle = Typeface.ITALIC;
} else if ("normal".equals(fontStyleString)) {
fontStyle = Typeface.NORMAL;
}

Typeface currentTypeface = view.getTypeface();
if (currentTypeface == null) {
currentTypeface = Typeface.DEFAULT;
}
if (fontStyle != currentTypeface.getStyle()) {
view.setTypeface(currentTypeface, fontStyle);
}
view.setFontStyle(fontStyleString);
}

@HippyControllerProps(name = NodeProps.FONT_WEIGHT, defaultType = HippyControllerProps.STRING, defaultString = "normal")
@HippyControllerProps(name = NodeProps.FONT_WEIGHT, defaultType = HippyControllerProps.STRING)
public void setFontWeight(HippyTextInput view, String fontWeightString) {
int fontWeightNumeric = fontWeightString != null ? parseFontWeight(fontWeightString) : -1;
int fontWeight = -1;
if (fontWeightNumeric >= 500 || "bold".equals(fontWeightString)) {
fontWeight = Typeface.BOLD;
} else //noinspection ConstantConditions
if ("normal".equals(fontWeightString) || (fontWeightNumeric != -1
&& fontWeightNumeric < 500)) {
fontWeight = Typeface.NORMAL;
}
Typeface currentTypeface = view.getTypeface();
if (currentTypeface == null) {
currentTypeface = Typeface.DEFAULT;
}
if (fontWeight != currentTypeface.getStyle()) {
view.setTypeface(currentTypeface, fontWeight);
}
view.setFontWeight(fontWeightString);
}

@HippyControllerProps(name = NodeProps.FONT_FAMILY, defaultType = HippyControllerProps.STRING, defaultString = "normal")
@HippyControllerProps(name = NodeProps.FONT_FAMILY, defaultType = HippyControllerProps.STRING)
public void setFontFamily(HippyTextInput view, String fontFamily) {
if (TextUtils.isEmpty(fontFamily)) {
return;
}
int style = Typeface.NORMAL;
if (view.getTypeface() != null) {
style = view.getTypeface().getStyle();
}
Typeface newTypeface = Typeface.create(fontFamily, style);
view.setTypeface(newTypeface);
view.setFontFamily(fontFamily);
}

private static final InputFilter[] EMPTY_FILTERS = new InputFilter[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@

public class TextStyleSpan extends MetricAffectingSpan {

private final int mStyle;
private final boolean mItalic;
private final int mWeight;
private final String mFontFamily;
private final FontAdapter mFontAdapter;

public TextStyleSpan(int fontStyle, int fontWeight, String fontFamily,
public TextStyleSpan(boolean italic, int fontWeight, String fontFamily,
FontAdapter adapter) {
mStyle = fontStyle;
mItalic = italic;
mWeight = fontWeight;
mFontFamily = fontFamily;
mFontAdapter = adapter;
}

@Override
public void updateDrawState(TextPaint textPaint) {
TypeFaceUtil.apply(textPaint, mStyle, mWeight, mFontFamily, mFontAdapter);
TypeFaceUtil.apply(textPaint, mItalic, mWeight, mFontFamily, mFontAdapter);
}

@Override
public void updateMeasureState(TextPaint textPaint) {
TypeFaceUtil.apply(textPaint, mStyle, mWeight, mFontFamily, mFontAdapter);
TypeFaceUtil.apply(textPaint, mItalic, mWeight, mFontFamily, mFontAdapter);
}
}
Loading

0 comments on commit bb9b7db

Please sign in to comment.