开发者

How should I troubleshoot a custom font that doesn't seem to load on Android

开发者 https://www.devze.com 2023-03-01 14:32 出处:网络
I have a custom font that won\'t seem to load on Android. Specifically I load it in the normal manner from an Activity, and then set it on a TextView:

I have a custom font that won't seem to load on Android. Specifically I load it in the normal manner from an Activity, and then set it on a TextView:

AssetManager assetManager = getAssets();
Typeface tf = Typeface.createFromAsset(assetManager, "euro.ttf");      
textView.setTypeface(tf);

What I get doesn't match the custom font at all, and there don't seem to be any errors (I've looked through logcat and didn't notice anything).

I've read an old article that says Android has issues with some fonts and silently fails over to Droid Sans.

Note that Android d开发者_StackOverflowoes not seem to like all TrueType fonts. I spent a lot of time trying to figure out what I was doing wrong with this sample, before switching to a different font, and everything "just worked". When Android dislikes a custom font, rather than raise an Exception, it seems to substitute Droid Sans ("sans") quietly. So, if you try to use a different font and it does not seem to be working, it may be that the font in question is incompatible with Android, for whatever reason.

So my question is, does anybody know how I can troubleshoot this further. Or what the "whatever reason" may be? I've tried different names for the font file, and even different copies of it. The font file seems to work fine on my desktop (I can open it with OS X and see the characters, they look correct). Also, using a different ttf file for another font does seem to work (so it doesn't seem to be setup/code related, rather it must be something with the file itself).


Sorry, I haven't run into anything more on this since I wrote that post nearly (gasp!) three years ago.

If you have access to a font editor, you might consider opening the font file and re-saving it, to see if that clears up whatever issues may be in there.

Or, if there's some utility floating around that basically does a "glyph diff" -- reporting which glyphs are in font A and not font B -- you might see if there's something interesting missing in the font you're trying. From what I've seen, a missing glyph normally just results in a garbage character (e.g., empty square), but maybe there's something special Android depends upon that your desired font does not have.

Push come to proverbial shove, if you have access to a font editor, you could try copying glyphs from the failing font file to a new font file and see if you wind up with something that works.

:: insert obligatory watch-for-font-licensing-issues blurb here ::

I seem to recall it took me a few tries to find a free font that worked, back in '08.

0

精彩评论

暂无评论...
验证码 换一张
取 消