Android - Using Custom Font

后端 未结 21 2026
别跟我提以往
别跟我提以往 2020-11-22 04:38

I applied a custom font to a TextView, but it doesn\'t seems to change the typeface.

Here is my code:

    Typeface myTypeface = Typeface         


        
21条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 05:26

    Update answer: Android 8.0 (API level 26) introduces a new feature, Fonts in XML. just use the Fonts in XML feature on devices running Android 4.1 (API level 16) and higher, use the Support Library 26.

    see this link


    Old answer

    There are two ways to customize fonts :

    !!! my custom font in assets/fonts/iran_sans.ttf



    Way 1 : Refrection Typeface.class ||| best way

    call FontsOverride.setDefaultFont() in class extends Application, This code will cause all software fonts to be changed, even Toasts fonts

    AppController.java

    public class AppController extends Application {
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            //Initial Font
            FontsOverride.setDefaultFont(getApplicationContext(), "MONOSPACE", "fonts/iran_sans.ttf");
    
        }
    }
    

    FontsOverride.java

    public class FontsOverride {
    
        public static void setDefaultFont(Context context, String staticTypefaceFieldName, String fontAssetName) {
            final Typeface regular = Typeface.createFromAsset(context.getAssets(), fontAssetName);
            replaceFont(staticTypefaceFieldName, regular);
        }
    
        private static void replaceFont(String staticTypefaceFieldName, final Typeface newTypeface) {
            try {
                final Field staticField = Typeface.class.getDeclaredField(staticTypefaceFieldName);
                staticField.setAccessible(true);
                staticField.set(null, newTypeface);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    



    Way 2: use setTypeface

    for special view just call setTypeface() to change font.

    CTextView.java

    public class CTextView extends TextView {
    
        public CTextView(Context context) {
            super(context);
            init(context,null);
        }
    
        public CTextView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            init(context,attrs);
        }
    
        public CTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init(context,attrs);
        }
    
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        public CTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
            init(context,attrs);
        }
    
        public void init(Context context, @Nullable AttributeSet attrs) {
    
            if (isInEditMode())
                return;
    
            // use setTypeface for change font this view
            setTypeface(FontUtils.getTypeface("fonts/iran_sans.ttf"));
    
        }
    }
    

    FontUtils.java

    public class FontUtils {
    
        private static Hashtable fontCache = new Hashtable<>();
    
        public static Typeface getTypeface(String fontName) {
            Typeface tf = fontCache.get(fontName);
            if (tf == null) {
                try {
                    tf = Typeface.createFromAsset(AppController.getInstance().getApplicationContext().getAssets(), fontName);
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
                fontCache.put(fontName, tf);
            }
            return tf;
        }
    
    }
    

提交回复
热议问题