Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Paint.setTypeface isn't working for italic

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...

like image 751
A. Masson Avatar asked May 25 '11 12:05

A. Masson


3 Answers

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

paint.setTextSkewX(-0.25f);
like image 145
dattia Avatar answered Nov 19 '22 02:11

dattia


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.

like image 35
styanton Avatar answered Nov 19 '22 01:11

styanton


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)?

like image 1
Ayaz Alifov Avatar answered Nov 19 '22 03:11

Ayaz Alifov