2017-02-02 6 views
0

カスタムフォントファミリでTextViewを作成しようとしています。私は通常のフォントをサポートするフォントを作成しました。しかし、太字/italicカスタムでのスタイリングTextViewを実装することができません。太字、標準、イタリック体のカスタムテキストビュー

この私のカスタムTextViewクラス

public class KTextView extends TextView { 

    public KTextView(Context context) { 
     super(context); 
     TypefaceHelper.setTypeface(context, this); 
    } 

    public KTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     TypefaceHelper.setTypeface(context, this); 
    } 

    public KTextView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     TypefaceHelper.setTypeface(context, this); 
    } 

} 

これは、誰もが太字や斜体のスタイルを実装する方法を提案することができますTypefaceHelperクラス

public class TypefaceHelper { 
private static final String FONT = "AvenirNext-Regular.otf"; 

private static Typeface typeface = null; 

public static void setTypeface(Context context, TextView textView) { 
    if (typeface == null) { 
     typeface = Typeface.createFromAsset(context.getAssets(), FONT); 
    } 
    textView.setTypeface(typeface); 
} 

です。

+0

これを参照してください:[実行時のTextViewのスタイルを変更する方法]( http://stackoverflow.com/questions/4630440/how-to-change-a-textviews-style-at-runtime) – FarshidABZ

答えて

1

これを試してみてください。

textview.setTypeface(typeface, Typeface.BOLD); 

カスタムTextViewの使用のためにもITALICBOLD_ITALIC

を使用することができます。

public class MyTextView extends TextView { 

     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(Typeface.createFromAsset(getContext().getAssets(), "fonts/fonts_name")/*, -1*/); 
      } else { 
       super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/fonts_name")/*, -1*/); 
      } 
     } 
} 
関連する問題