如果在App设计过程中,有需求要用第三方的ttf字体,那么在项目初期,最好使用一个自定义的TextVIew来做这件事,以免哪天产品狗突然发疯让你换字体的话 ,在项目中一个一个的TextView的去换TypeFace可是要了亲命了.......
那么,如何写这个自定义的TextView呢?看代码........
package com.hackvg.android.views.custom_views;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created by saulmm on 25/01/15.
*/
public class LobsterTextView extends TextView {
public LobsterTextView(Context context) {
super(context);
init(context);
}
public LobsterTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public LobsterTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
Typeface t = Typeface.createFromAsset(context.getAssets(), "Lobster-Regular.ttf");
this.setTypeface(t);
}
}
然后,把你下载好的TTF文件放到assets文件夹下就好了,以后如果要换字体,只要把自定义的TextView中的字体换掉即可。
是不是很安心呢?
产品狗 你来吧!