TextAppearanceSpan

字体样式。

构造函数:

  • TextAppearanceSpan(Context context, int appearance):参数context,传入的上下文,参数appearance,引用的样式表,如R.style.my_style。
  • TextAppearanceSpan(Context context, int appearance, int colorList): 参数context,使用的上下文,参数appearance,引用的样式表,如R.style.my_style, 参数colorList,使用方式未知,如果设置为小于0,则参数将不产生效果。
  • TextAppearanceSpan(String family, int style, int size, ColorStateList color, ColorStateList linkColor): 参数family,字体,仅支持系统自带的三种字体,MONOSPACE、SERIF和SANS, 参数style,TypeFace中定义的字体样式,BOLD、ITALIC等, 参数size,字体大小,参数color,字体颜色,参数linkColor,使用方式未知。
  • TextAppearanceSpan(Parcel src):参数src,含有样式信息的包装类

示例1:

TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(context, R.style.xx);

String text = "打开百度";
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(textAppearanceSpan, 2, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(spannableString);

示例2:

Parcel parcel = Parcel.obtain();
parcel.writeString("SERIF");
parcel.writeInt(Typeface.BOLD_ITALIC);
parcel.writeInt(18);
try {
    ColorStateList colorList = ColorStateList.createFromXml(context.getResources(), context.getResources().getXml(R.drawable.color_list));
    parcel.writeInt(1);
    colorList.writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
    parcel.writeInt(1);
    colorList.writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
} catch (Exception e) {
    e.printStackTrace();
}
parcel.setDataPosition(0);

TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(parcel);

String text = "打开百度";
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(textAppearanceSpan, 2, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(spannableString);

示例3:

重写updateDrawState(TextPaint textPaint)方法,修改字体的样式、文本颜色:

TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(this, R.style.xx) {
    @Override
    public void updateDrawState(TextPaint textPaint) {
        super.updateDrawState(textPaint);
        textPaint.setColor(Color.argb(255, 54, 92, 124));
    }
};

String text = "打开百度";
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(typefaceSpan, 2, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(spannableString);