Android Paint.setTypeface isn't working for italic
Asked Answered
L

3

10

The Paint.setTypeface is not working for italic or I'm doing something the wrong way. I can create normal, bold, monospace, and serif text, but I can't create italic text. It always looks normal (or in the case of bold-italic, it looks bold).

    //This will appear monospace
    paint.setTypeface(Typeface.MONOSPACE);
    canvas.drawText("foo", 10, 10, paint);

    //This will appear serif
    paint.setTypeface(Typeface.SERIF);
    canvas.drawText("foo", 10, 10, paint);

    //This will appear bold
    paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    canvas.drawText("foo", 10, 10, paint);

    //This will NOT appear italic <===  PROBLEM
    paint.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
    canvas.drawText("foo", 10, 10, paint);

    // This isn't working either <===  PROBLEM
    paint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.ITALIC));

So now the question: is there a known workaround for this? My simple goal is to draw some words with italic style...

Luca answered 25/5, 2011 at 12:28 Comment(0)
E
14

After experiencing the same difficulty, I found the solution by fishing around in theTextViewsource code. Try this:

paint.setTextSkewX(-0.25f);
Elum answered 5/6, 2012 at 16:5 Comment(1)
Before using setTextSkewX method, set the TypeFace to normal, otherwise a text will be skewed much more than needed on devices which support ITALIC mode for a default font. So, use like this: paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL); paint.setTextSkewX(-0.25f);Misery
B
8

I have the same problem. looks like not all android typefaces supports ITALIC style. Try following, i'ts worked for me:

paint.setTypeface(Typeface.create(Typeface.SERIF,Typeface.ITALIC));

Works fine just with SERIF. DEFAULT, MONOSPACE, SANS_SERIF ingnores this style.

P.S. I'm talking about API 10.

Bryophyte answered 12/5, 2012 at 10:54 Comment(1)
Yes if your target is API 10 and above because Google fixed the problem. So this solutions is fine too. Howerver some of my apps still have to be API 4 compatible...Luca
M
1

In order to get an italic mode for devices which don't support it for a default font, we should use setTextSkewX method. However, before applying it we have to be sure that an italic mode is not supported. We achieve it by creating a temporal TextView object and measuring its width in both modes (NORMAL and ITALIC). If their widths are same, then it means an ITALIC mode is NOT supported.

Please, take a look at a solution presented in another question: Samsung devices supporting setTypeface(Typeface.Italic)?

Misery answered 6/10, 2015 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.