如果在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中的字體換掉即可。
是不是很安心呢?
產品狗 你來吧!