Android 字体

来源:互联网 发布:亚马逊云计算平台 编辑:程序博客网 时间:2024/05/16 14:13

1.typeface替换字体

Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "iconfont/iconfont.ttf");flow_phone_tv.setTypeface(typeface);
2.SpannableStringBuilder  中使用自定义字体

Typeface font = Typeface.createFromAsset(getAssets(), "bangla.ttf");   SpannableStringBuilder SS = new SpannableStringBuilder("আমারநல்வரவு");SS.setSpan (new CustomTypefaceSpan("", font), 4, 11,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);txt.setText(SS);
其中的CustomTypefaceSpan是一个自定义类,如下:

import android.graphics.Paint;import android.graphics.Typeface;import android.text.TextPaint;import android.text.style.TypefaceSpan;public class CustomTypefaceSpan extends TypefaceSpan {private final Typeface newType;public CustomTypefaceSpan(String family, Typeface type) {    super(family);    newType = type;}@Overridepublic void updateDrawState(TextPaint ds) {    applyCustomTypeFace(ds, newType);}@Overridepublic void updateMeasureState(TextPaint paint) {    applyCustomTypeFace(paint, newType);}private static void applyCustomTypeFace(Paint paint, Typeface tf) {    int oldStyle;    Typeface old = paint.getTypeface();    if (old == null) {        oldStyle = 0;    } else {        oldStyle = old.getStyle();    }    int fake = oldStyle & ~tf.getStyle();    if ((fake & Typeface.BOLD) != 0) {        paint.setFakeBoldText(true);    }    if ((fake & Typeface.ITALIC) != 0) {        paint.setTextSkewX(-0.25f);    }    paint.setTypeface(tf);}}


0 0
原创粉丝点击