Skip to content

Instantly share code, notes, and snippets.

@jackmahoney
Created February 24, 2014 04:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jackmahoney/9181787 to your computer and use it in GitHub Desktop.
Save jackmahoney/9181787 to your computer and use it in GitHub Desktop.
Set custom font on an android custom textview. Untested code, be sure to test.
public class TextHelper{
private static TypeFace typeface = null;
public static void setTypeface(Context context, TextView textview){
if(this.typeface == null){
this.typeface = Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf");
}
textview.setTypeface(face);
}
}
public class FontTextView extends TextView {
public FontTextView(Context context) {
super(context);
TextHelper.setTypeface(context, this);
}
}
@lminhphuongtma
Copy link

Line 9 : textview.setTypeface(typeface) is more exactly

@traendy
Copy link

traendy commented May 12, 2017

so smth like this?
`
class TextHelper{

private  static Typeface  typeface = null;

public static void setTypeface(Context context, TextView textview){
    if(typeface == null){
        typeface = Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf");
    }
    textview.setTypeface(typeface);
}

}

public class CustomTextView extends TextView {

public CustomTextView(Context context){
    super(context);
    TextHelper.setTypeface(context, this);

}
public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    TextHelper.setTypeface(context, this);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    TextHelper.setTypeface(context, this);
}
protected void onDraw (Canvas canvas) {
    super.onDraw(canvas);


}

}
`

@qwerty9849
Copy link

Nice , Thanks

@shahrukhbaig786
Copy link

shahrukhbaig786 commented Mar 26, 2018

Great !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment