Android Paint.setTypeface isn't working for italic

A. Masson picture A. Masson · May 25, 2011 · Viewed 14.1k times · Source

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

Answer

dattia picture dattia · Jun 5, 2012

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

paint.setTextSkewX(-0.25f);