1

I am doing the below. All I get is the basic font, not my custom symbol font.

Any ideas?

     Paint pnt = new Paint();
    // SymbolNo is 38. Returns string "&" which is correct in normal font.
    String symbolText = Character.toString((char)SymbolNo); 

    // Should adopt a symbol font and draw symbol to screen instead. But I just see "&"
    Typeface tf = Typeface.createFromAsset(m_context.getAssets(), "fonts/myFont.TTF" ); 
    pnt.setTypeface(tf);

    m_canvas.drawText(symbolText,x, y, pnt);

my font is in assets/fonts/myFont.TTF

Simon Sarris
  • 62,212
  • 13
  • 141
  • 171
IAmGroot
  • 13,760
  • 18
  • 84
  • 154

2 Answers2

1

Not every font works with Android. It just silently fails.

One course of action is to find an app that definitely handles a custom font -- such as this sample app of mine -- as a basis for experimentation. You can run that app to confirm that its fonts appear, then replace one of those with your font. If that works, then there is something messed up in the way you are loading in the font (though I have no idea what or how). If the font fails to work in my sample app, where the font that ships with that app does work, the problem lies in the font.

Unfortunately, I have no idea what makes a font work or not work. You could try opening the font in a font editor, making a minor change (e.g., deleting some glyph you know that you won't need), saving it back out, and seeing if the revised font works. If it does, that means that however the font was saved originally has something in it that Android does not like, but that your font editor can generate Android-friendly fonts.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I'll give your suggestions a try. Does it matter that not all the chars in the font file have an actual mapped char. Aka, I see a lot of those blank place holder rectangles. – IAmGroot Nov 08 '12 at 12:35
  • @Doomsknight: Possibly, though since I see those from time to time with fonts that otherwise work, I am not aware of a general problem with missing glyphs. It is conceivable that there are some special values that need a glyph (e.g., ASCII 32 for space), where if that glyph is missing, it breaks the font for Android. I have yet to stumble across a definitive reason (or set of reasons) why some fonts fail silently this way. – CommonsWare Nov 08 '12 at 12:49
  • I am able to copy the char from one charset, to a new one, and it works fine. Unfornately the font program I use has rubbish copy features. And would take me far too long to copy one by one. I'll keep looking for a better one. Don't suppose you know of any. Thanks for the answer. Something in this 1995 font is causing the problem. – IAmGroot Nov 08 '12 at 14:43
  • @Doomsknight: I know that I have used a font editor for Linux, but I'm not seeing it in my menus, so I forget which one I used. And, I didn't use it for much in the first place, so I have no idea how extensive its capabilities were. TL;DR: I don't have a good recommendation for you, sorry. – CommonsWare Nov 08 '12 at 14:44
  • Do you think the platform "Windows Symbol" char set, is the problem? The ones I create and worked are "Windows Unicode BMP". Its all strange. Or infact, have I mapped wrong, $F027 -> $0026. onto Ampersand. As the file reads it to be value 38.. But is there an offset to add for symbols? aka am I actually using the wrong char value. – IAmGroot Nov 08 '12 at 15:12
  • 1
    @Doomsknight: "Do you think the platform "Windows Symbol" char set, is the problem?" -- it has "Windows" in the name, so I'm up for blaming it. :-) With respect to the rest of your comment, I have never used a symbol font in an app, so I have no clue. Sorry. – CommonsWare Nov 08 '12 at 15:59
-3

Hello I have a solution regarding this can you try using fonts in this way ... I Have implemented this in my Project ...

Steps:

  1. Make a Package (com.fontUtils.fonts)
  2. Make the Font Files Like For TextView , EditText or Button Text

For Example :

    public class ButtonHelveticaBold extends Button {

        Context ButtonFontContext;

        public ButtonHelveticaBold(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            ButtonFontContext = context;
        }

        public ButtonHelveticaBold(Context context, AttributeSet attrs) {
            super(context, attrs);
            ButtonFontContext = context;
        }

        public ButtonHelveticaBold(Context context) {
            super(context);
            ButtonFontContext = context;
        }

        @Override
        public void setTypeface(Typeface tf, int style) {
            Typeface typeFaceHelvetica;
            if (style == Typeface.BOLD) {
                typeFaceHelvetica = Typeface.createFromAsset(getContext().getAssets(), "fonts/helvetica_bold_neue.ttf");
            } else {
                typeFaceHelvetica = Typeface.createFromAsset(getContext().getAssets(), "fonts/helvetica_neue_regular.ttf");
            }
            super.setTypeface(typeFaceHelvetica);
        }

    }

3 : Use this in XML Like this way:

   <com.fontUtils.fonts.ButtonHelveticaBold 
      android:id="@+id/btn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      />
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
Jay Thakkar
  • 743
  • 1
  • 5
  • 24
  • the line `typeFaceHelvetica = Typeface.createFromAsset(getContext().getAssets(), "fonts/helvetica_neue_regular.ttf");` is in your solution, but is the problem. It seems like you are just wrapping it. But the underlying problem will still exist in my scenario. – IAmGroot Nov 08 '12 at 13:31
  • Not Getting You I am giving you the solution How to use Custom Fonts – Jay Thakkar Nov 08 '12 at 13:40
  • What you have provided is a custom button class, that sets a custom font exactly the same way that I do. (so would fail for my font too). – IAmGroot Nov 08 '12 at 14:05
  • No this is valid for all For TextView also if u want to implement on that ... For anyone in EditText also – Jay Thakkar Nov 08 '12 at 14:18
  • The benefit of this is only that you dont have to declare typeface like the way you used so this feels is better way for using Fonts – Jay Thakkar Nov 08 '12 at 14:19
  • Your using `createFromAsset` the same way I do! I know what your code is doing, but it is not what I have problems with, nor asked for. Thanks anyway. – IAmGroot Nov 08 '12 at 14:40