gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:13:17 29 4
gpt4 key购买 nike

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

最佳答案

嗯,我不知道如何使用可用的类,所以我自己扩展了 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);
}
}

关于android - 如何将 TypefaceSpan 或 StyleSpan 与自定义字体一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4819049/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com