Custom Font not working on Android

2019-02-20 16:18发布

问题:

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

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



回答2:

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"
      />