Custom Fonts and Custom Textview on Android

匿名 (未验证) 提交于 2019-12-03 09:02:45

问题:

From an application I need to develop, I've received a specific font that has many files like FontName-Regular, FontName-Bold, FontName-it. I need to use it in all the textviews in the application. First I thought it was an easy task. Look over SO and found a very nice thread:here

So first I did like:

public static void overrideFonts(final Context context, final View v) {     try {         if (v instanceof ViewGroup) {             ViewGroup vg = (ViewGroup) v;             for (int i = 0; i 

And called this method during onCreate in my activity. Every textView in my app was showing that font and boy, was I happy for getting away so easy. Until I got to a screen where some textviews required Bold as Style (android:textStyle="bold"). Then I realized that this solution does not provide me with possibility to load the Font-Bold.ttf from assets.

Than looked further and saw a nice custom TextView implementation, in the same SO question:

public class MyTextView extends TextView {      public MyTextView(Context context, AttributeSet attrs, int defStyle) {         super(context, attrs, defStyle);         init();     }      public MyTextView(Context context, AttributeSet attrs) {         super(context, attrs);         init();     }      public MyTextView(Context context) {         super(context);         init();     }      public void init() {          Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/chiller.ttf");         setTypeface(tf ,1);      }     } 

This looks even better. My question is: how can I detect on init() if my control has Style set to Bold or not so I can assign the requested TypeFace ?

Thank you for your time.

LE. Following the example below, I've updated my class as:

public class MyTextView extends TextView {      Typeface normalTypeface = Typeface.createFromAsset(getContext().getAssets(), Constants.FONT_REGULAR);     Typeface boldTypeface = Typeface.createFromAsset(getContext().getAssets(),  Constants.FONT_BOLD);      public MyTextView(Context context, AttributeSet attrs, int defStyle) {         super(context, attrs, defStyle);     }      public MyTextView(Context context, AttributeSet attrs) {         super(context, attrs);     }      public MyTextView(Context context) {         super(context);     }      public void setTypeface(Typeface tf, int style) {         if (style == Typeface.BOLD) {             super.setTypeface(boldTypeface/*, -1*/);         } else {             super.setTypeface(normalTypeface/*, -1*/);         }     } } 

Well If I debug, the app goes in setTypeFace and it seems to apply the bold one, but on my layout I can't see any change, not bold. No matter what font I use, no changes are done in my TextView and is displayed with the default android font. I wonder why ?

I have summed everything on a blog post here on my blog maybe it will help someone.

回答1:

The constructor of TextView calls setTypeface(Typeface tf, int style) with the style parameter retrieved from the XML attribute android:textStyle. So, if you want to intercept this call to force your own typeface you can override this method as follow:

public void setTypeface(Typeface tf, int style) {     Typeface normalTypeface = Typeface.createFromAsset(getContext().getAssets(), "font/your_normal_font.ttf");     Typeface boldTypeface = Typeface.createFromAsset(getContext().getAssets(), "font/your_bold_font.ttf");      if (style == Typeface.BOLD) {         super.setTypeface(boldTypeface/*, -1*/);     } else {         super.setTypeface(normalTypeface/*, -1*/);     } } 


回答2:

You can use my CustomTextView which allows you to specify a font file name in your assets folder:

https://github.com/mafshin/CustomTextView

and the usage is really simple:



回答3:

I think it's better to create your own package for custom fonts and import them in your project so that you can use them later in future

  package com.codeslips.utilities;      import android.content.Context;    import android.graphics.Typeface;   import android.util.AttributeSet;   import android.widget.TextView;      public class CustomTextView extends TextView {              public CustomTextView(Context context)                { super(context); setFont(); }              public CustomTextView(Context context,AttributeSet set)              { super(context,set); setFont(); }             public CustomTextView(Context context,AttributeSet set,int defaultStyle)               { super(context,set,defaultStyle); setFont(); }             private void setFont() {              Typeface typeface=Typeface.createFromAsset(getContext().getAssets(),"fonts/your-font.ttf");             setTypeface(typeface); //function used to set font               }             } 

Now use the above class in your XML file to have your custom font

    


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!