Samsung devices supporting setTypeface(Typeface.Italic)?

亡梦爱人 提交于 2019-11-29 06:15:20

It may be that your Samsung device does not have a native italics version of the desired font installed. You may have to force the system to create the italics-style font synthetically. Try:

tv.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC), Typeface.ITALIC);

EDIT

Instead of defaultFromStyle, try to use Typeface.create (Typeface family, int style) (documented here).

Try passing direct values to the setTypeFace api till you find the right one. If italicizing is working through other methods then there could be some problem in constant definitions in TypeFace class (in those builds).

mPaintText.setTypeface(Typeface.defaultFromStyle(0)); // then 1, 2, 3

This is a bug from Samsung and the best solution is, as FomayGuy said, to add the italic version of the system font to the assets.

The official Roboto Android font is available here.

We need to check whether a default font supports an ITALIC mode. We do it by creating a temporal TextView object and measuring its width in both modes (NORMAL and ITALIC). If their widths are different, then it means an ITALIC mode is supported. Otherwise, a default font doesn't support it and we have to use setTextSkewX() method to skew a text.

    mPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.ITALIC));

    // check whether a font supports an italic mode, returns false if it does't
    if (!supportItalicMode(this, Typeface.DEFAULT))
    {
        paint.setTextSkewX(-0.25f);
    }



private boolean supportItalicMode(Context context, Typeface typeFace)
{
    Typeface tfNormal = Typeface.create(typeFace, Typeface.NORMAL);
    Typeface tfItalic = Typeface.create(typeFace, Typeface.ITALIC);

    TextView textView = new TextView(context);
    textView.setText("Some sample text to check whether a font supports an italic mode");

    textView.setTypeface(tfNormal);
    textView.measure(0, 0);
    int normalFontStyleSize = textView.getMeasuredWidth();

    textView.setTypeface(tfItalic);
    textView.measure(0, 0);
    int italicFontStyleSize = textView.getMeasuredWidth();

    return (normalFontStyleSize != italicFontStyleSize);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!