首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单个TextView中的多个TypeFace

单个TextView中的多个TypeFace
EN

Stack Overflow用户
提问于 2012-05-21 00:15:49
回答 2查看 21.5K关注 0票数 41

我想用TypeFace设置TextView上的第一个字符,用不同的字体设置第二个字符,依此类推。

我读了这个例子:

代码语言:javascript
复制
Spannable str = (Spannable) textView.getText();
str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, 7  
                             ,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

但这对我没有帮助,因为我想设置多个TypeFace (external TTFs)

有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-24 23:39:54

使用以下代码:(我使用的是孟加拉和泰米尔字体)

代码语言:javascript
复制
  TextView txt = (TextView) findViewById(R.id.custom_fonts);  
        txt.setTextSize(30);
        Typeface font = Typeface.createFromAsset(getAssets(), "Akshar.ttf");
        Typeface font2 = Typeface.createFromAsset(getAssets(), "bangla.ttf");   
        SpannableStringBuilder SS = new SpannableStringBuilder("আমারநல்வரவு");
        SS.setSpan (new CustomTypefaceSpan("", font2), 0, 4,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        SS.setSpan (new CustomTypefaceSpan("", font), 4, 11,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        txt.setText(SS);

结果是:

它使用取自How can I use TypefaceSpan or StyleSpan with a custom Typeface?CustomTypefaceSpan

代码语言:javascript
复制
package my.app;
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);
}
}
票数 123
EN

Stack Overflow用户

发布于 2020-09-01 16:24:47

Kotlin开发人员可以使用它来为单按钮或文本视图使用两种或两种以上的字体。

1.复制并粘贴此类:

代码语言:javascript
复制
class CustomTypefaceSpan(family: String?, private val newType: Typeface) : TypefaceSpan(family) {

    override fun updateDrawState(ds: TextPaint) {
        applyCustomTypeFace(ds, newType)
    }

    override fun updateMeasureState(paint: TextPaint) {
        applyCustomTypeFace(paint, newType)
    }

    companion object {
        private fun applyCustomTypeFace(paint: Paint, tf: Typeface) {
            val oldStyle: Int
            val old = paint.typeface
            oldStyle = old?.style ?: 0
            val fake = oldStyle and tf.style.inv()
            if (fake and Typeface.BOLD != 0) {
                paint.isFakeBoldText = true
            }
            if (fake and Typeface.ITALIC != 0) {
                paint.textSkewX = -0.25f
            }
            paint.typeface = tf
        }
    }
}

2.使用者:

代码语言:javascript
复制
val buttonText = "Get it for 100 Points"

//making text yellow
    buttonText.setSpan(
        ForegroundColorSpan(
            ContextCompat.getColor(
                this, R.color.yellow_FFC001
            )
        ),
        11, 14,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
    )

//changing text size of yellow portion i.e. "100"
    val sp20 = resources.getDimensionPixelSize(R.dimen.sp_20)
    buttonText.setSpan(AbsoluteSizeSpan(sp20), 11, 14, Spannable.SPAN_INCLUSIVE_INCLUSIVE)

//setting button fonts
    buttonText.setSpan(
        CustomTypefaceSpan("", ResourcesCompat.getFont(this, R.font.roboto_condensed_bold)!!),
        0,
        buttonText.length,
        0
    )

//finally setting this text to my button
btGetPoints.text = buttonText

3.最终输出将类似于下面的

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10675070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档