Custom Font not working on Android

前端 未结 2 661
小蘑菇
小蘑菇 2021-01-16 11:13

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         


        
相关标签:
2条回答
  • 2021-01-16 11:54

    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"
          />
    
    0 讨论(0)
  • 2021-01-16 12:01

    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.

    0 讨论(0)
提交回复
热议问题