33

I am using a custom .ttf font in my android app. I load it the usual way:

myTypeface = Typeface.createFromAsset( getAssets(), "myTypeface.ttf");

then I assign my typeface within my activity... pretty straightforward stuff:

TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setTextSize(12);
tv.setTypeface(App.myTypeface);

The problem I'm running into is that on some devices using later APIs (I've specifically noticed it in an emulator for the Asus Transformer), the text looks slightly bolder, less uniform in width, and more jumbled in vertical alignment. By that last part I mean that some characters are placed vertically a bit higher or lower than others, giving a little bit of a roller-coaster feel to the text.

Consider the screen shots below

This is text rendered on an emulator with the same resolution and dpi as a Transformer, but using Google API level 8.

i45.tinypic.com/142toud.png

Looks pretty standard, right?

Now consider the text rendered in an emulator with the same resolution and dpi, but using Google API level 15:

i47.tinypic.com/24zhekn.png

At first the text may look similar, though you might notice it seems a bit bolder. However, look at the "c" in "quick". You will notice that it sits lower, and is taller, than the "c" in the first rendering. You will also notice that if you look at the bottom of the characters in the word "quick", they are not aligned on the bottom.

These issues may seem small, but on screens with lots of text, it starts to look really unprofessional.

Anybody seen this, or have an explanation? I'd love to make the text look uniform in later APIs.

Thanks so much for your time!

benjamin davis
  • 680
  • 6
  • 12
  • Definitely some different font rendering going on. Could be antialiasing, hinting turned off, possibly. Could you see what the value for `textView.getPaintFlags()` returns for both API level 8, and API level 15, and edit it in? (@ me so I get a notification and I'll take a look) – Kevin Coppock Jun 08 '12 at 17:12
  • @kcoppock Thanks for the reply. getPaintFlags() returns 257 for both APIs. – benjamin davis Jun 08 '12 at 17:27
  • This looks similar, or maybe a duplicate of http://stackoverflow.com/questions/9036184/custom-font-rendering-on-android-4-0-ice-cream-sandwich. It was never truly answered. – HandlerExploit Jun 08 '12 at 17:35
  • @HandlerExploit Yes, I'd looked at that posting when I was searching the forums for this issue. It does seem similar, but I didn't think they ever really specified what they didn't like about the font... and it was never answered, so I figured I'd ask specifically. =) Thanks for pointing it out, though. – benjamin davis Jun 08 '12 at 17:41
  • I am the one who asked that question and it sounds like the exact issue we were seeing. The only difference is I didn't go into exacts like you did. – HandlerExploit Jun 08 '12 at 17:44

1 Answers1

59

Okay, so it seems to have just the following flags applied in both instances:

Paint.DEV_KERN_TEXT_FLAG
Paint.ANTI_ALIAS_FLAG

Try doing this, and see if the results are any different (not necessarily improved, but even noticeable at all):

textView.setPaintFlags(textView.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • 1
    Huge difference! The text actually looks correct! Thanks so much for the input. Here's a shot of the two emulators now, Transformer on the left: ![New Text Rendering](http://i49.tinypic.com/nwzzaw.png) – benjamin davis Jun 08 '12 at 17:58
  • That's great! I thought that might do the trick. I would say they'd disabled that by default on the newer API, but you were getting the same flag values... I'm not sure WHY exactly this works, but I'm glad it does. :) If you think this is all you need, you can mark this question as answered. – Kevin Coppock Jun 08 '12 at 18:00
  • @HandlerExploit: Perhaps this might work for your problem as well? Might give it a try! :) – Kevin Coppock Jun 08 '12 at 18:15
  • the second part was enough for me. – Snicolas Feb 21 '13 at 05:47
  • 5
    +1 works for me - custom fonts were rendering correctly on 2.3 but not 4.2.2. Great help!!! – Dori Feb 22 '13 at 12:34
  • 5
    the same but with less text: textView.getPaint().setSubpixelText(true); – luben Dec 20 '13 at 10:57
  • 3
    @middlehut I wouldn't do that. The docs for [`getPaint()`](http://developer.android.com/reference/android/widget/TextView.html#getPaint()) say: "Please use this only to consult the Paint's properties and not to change them." – Jarett Millard Feb 01 '16 at 16:01