如何将Spannable对象字体设置为自定义字体

我有Spannable对象,我想通过我之前加载的自定义字体设置其字体。

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/font_Name.ttf"); Spannable span1 = /*Spannable Item*/; /// I want to set span1 to have tf font face??? /// Here where I want help. 

编辑:
我的问题是,我想为文本视图设置两个不同的自定义字体,所以我正在使用Spannable

这是一个迟到的答案,但会帮助别人解决这个问题。

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

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

结果是:

在这里输入图像描述


CustomTypefaceSpan类:

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

参考

如果您正在使用Roboto,则可以在构造函数中设置不同的TypefaceSpan

TypefaceSpan typefaceSpan = new TypefaceSpan("sans-serif-medium");

textView.setSpan(typefaceSpan, indexStart, textLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

创build一个CustomTypefaceSpan类:

 import android.graphics.Paint; import android.graphics.Typeface; import android.text.TextPaint; import android.text.style.MetricAffectingSpan; public class CustomTypefaceSpan extends MetricAffectingSpan { private final Typeface typeface; public CustomTypefaceSpan(Typeface typeface) { this.typeface = typeface; } @Override public void updateDrawState(TextPaint ds) { applyCustomTypeFace(ds, typeface); } @Override public void updateMeasureState(TextPaint paint) { applyCustomTypeFace(paint, typeface); } private static void applyCustomTypeFace(Paint paint, Typeface tf) { paint.setTypeface(tf); } } 

与Android框架跨越类一样使用:

  TextView textView = (TextView) findViewById(R.id.custom_fonts); Typeface font = Typeface.createFromAsset(getAssets(), "Akshar.ttf"); Typeface font2 = Typeface.createFromAsset(getAssets(), "bangla.ttf"); SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("আমারநல்வரவு"); spannableStringBuilder.setSpan (new CustomTypefaceSpan(font2), 0, 4,Spanned.SPAN_EXCLUSIVE_INCLUSIVE); spannableStringBuilder.setSpan (new CustomTypefaceSpan(font), 4, 11,Spanned.SPAN_EXCLUSIVE_INCLUSIVE); textView.setText(spannableStringBuilder); 

这个答案是基于Imran Rana的答案,但不扩展TypefaceSpan ,然后禁用其function。 MetricAffectingSpan直接扩展MetricAffectingSpan

这个答案与Imran Rana的答案有一个缺陷。 跨度不包括在内。 即如果你这样做(kotlin):

  val parcel = Parcel.obtain() TextUtils.writeToParcel(spannableStringBuilder, parcel, 0) parcel.setDataPosition(0) val sequence = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel) parcel.recycle() 

spannableStringBuilder设置的任何CustomTypefaceSpan对象都不会被编组CustomTypefaceSpan组。

尝试Spannable设置为TextView ,然后尝试使用myTextView.setTypeface(tf)将字体分配给TextView

这里有一个例子,str是你的完整string,boldString是你需要做粗体的部分。

 public static SpannableString getTextStyleSpan(String str, String boldString) { SpannableString formated = new SpannableString(str); int start1 = str.indexOf(boldString); int end1 = start1 + colorString1.length(); formated.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), start1, end1, Spannable.SPAN_INCLUSIVE_INCLUSIVE); return formated; }