如何将TypefaceSpan或StyleSpan与自定义字体一起使用?

我还没有找到一种方法来做到这一点。可能吗?

回答:

好吧,我无法弄清楚如何使用可用的类,因此TypefaceSpan我自己扩展了它,现在它对我有用。这是我所做的:

package de.myproject.text.style;

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;

}

@Override

public void updateDrawState(TextPaint ds) {

applyCustomTypeFace(ds, newType);

}

@Override

public 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);

}

}

以上是 如何将TypefaceSpan或StyleSpan与自定义字体一起使用? 的全部内容, 来源链接: utcz.com/qa/423396.html

回到顶部