Setting custom font to Textview

325 views Asked by At

I am using custom fonts in my application So I have used following code

public class BrandonBlackTextView extends TextView {

public BrandonBlackTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public BrandonBlackTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public BrandonBlackTextView(Context context) {
    super(context);
    init();
}

public void init() {
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/brandon_blk.ttf");
    setTypeface(tf ,1);

}

wherever I wants to use I use this textview. It works. But problem is sometimes I don't know why but text inside textview becomes bold. I don't know why this happening. And this is very inconsistent, it doesn't happen every time.

in the pic, you see in one row text is bold.

1

There are 1 answers

4
Karakuri On

Try adding this to your init:

int flags = getPaintFlags() | Paint.ANTI_ALIAS_FLAG |  Paint.SUBPIXEL_TEXT_FLAG;
setPaintFlags(flags);

Also you should cache the typeface somewhere instead of loading it again for every TextView.