SpannableStringBuilder可以用來(lái)實(shí)現(xiàn)富文本樣式讼载。
TextAppearanceSpan是系統(tǒng)提供的用來(lái)修改文本簡(jiǎn)單樣式:
- 設(shè)置不同的文字大小
- 設(shè)置不同的文字顏色
- 設(shè)置文字的字體
- 設(shè)置文字粗體
- 超鏈接顏色
fun testSpan() {
var builder = SpannableStringBuilder()
var price = "10,837"
var note = "當(dāng)前價(jià)"
builder.append("¥")
builder.append("${price} ")
builder.append(note)
//默認(rèn)字體,粗體毅该,文字大屑嘤摇:60边灭,紅色
var yuanSpan = TextAppearanceSpan(
null, Typeface.BOLD, 60,
ColorStateList.valueOf(resources.getColor(R.color._C6100E)), null
)
builder.setSpan(yuanSpan, 0, 1, SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE)
//默認(rèn)字體,粗體健盒,文字大腥奘荨:80,紅色
var priceSpan = TextAppearanceSpan(
null, Typeface.BOLD, 80,
ColorStateList.valueOf(resources.getColor(R.color._C6100E)), null
)
builder.setSpan(
priceSpan,
1,
price.length + 1,
SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE
)
//serif字體扣癣,正常惰帽,文字大小:60父虑,灰色
var noteSpan = TextAppearanceSpan(
"sans", Typeface.NORMAL, 60,
ColorStateList.valueOf(resources.getColor(R.color._888888)), null
)
builder.setSpan(
noteSpan,
builder.length - note.length,
builder.length,
SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE
)
findViewById<TextView>(R.id.tv_app).setText(builder)
}
效果如圖:
!
TextAppearanceSpan支持的屬性该酗,可以從源碼中獲得:
/**
* Uses the specified TextAppearance resource to determine the
* text appearance, and the specified text color resource
* to determine the color. The <code>appearance</code> should be,
* for example, <code>android.R.style.TextAppearance_Small</code>,
* and the <code>colorList</code> should be, for example,
* <code>android.R.styleable.Theme_textColorPrimary</code>.
*/
public TextAppearanceSpan(Context context, int appearance, int colorList) {
ColorStateList textColor;
TypedArray a =
context.obtainStyledAttributes(appearance,
com.android.internal.R.styleable.TextAppearance);
//字體顏色
textColor = a.getColorStateList(com.android.internal.R.styleable.
TextAppearance_textColor);
//超鏈接顏色
mTextColorLink = a.getColorStateList(com.android.internal.R.styleable.
TextAppearance_textColorLink);
//字體大小
mTextSize = a.getDimensionPixelSize(com.android.internal.R.styleable.
TextAppearance_textSize, -1);
//風(fēng)格:粗體、斜體士嚎、正常
mStyle = a.getInt(com.android.internal.R.styleable.TextAppearance_textStyle, 0);
//字體
if (!context.isRestricted() && context.canLoadUnsafeResources()) {
mTypeface = a.getFont(com.android.internal.R.styleable.TextAppearance_fontFamily);
} else {
mTypeface = null;
}
if (mTypeface != null) {
mFamilyName = null;
} else {
String family = a.getString(com.android.internal.R.styleable.TextAppearance_fontFamily);
if (family != null) {
mFamilyName = family;
} else {
int tf = a.getInt(com.android.internal.R.styleable.TextAppearance_typeface, 0);
switch (tf) {
case 1:
mFamilyName = "sans";
break;
case 2:
mFamilyName = "serif";
break;
case 3:
mFamilyName = "monospace";
break;
default:
mFamilyName = null;
break;
}
}
}
mTextFontWeight = a.getInt(com.android.internal.R.styleable
.TextAppearance_textFontWeight, -1);
final String localeString = a.getString(com.android.internal.R.styleable
.TextAppearance_textLocale);
if (localeString != null) {
LocaleList localeList = LocaleList.forLanguageTags(localeString);
if (!localeList.isEmpty()) {
mTextLocales = localeList;
} else {
mTextLocales = null;
}
} else {
mTextLocales = null;
}
//文字投影
mShadowRadius = a.getFloat(com.android.internal.R.styleable
.TextAppearance_shadowRadius, 0.0f);
mShadowDx = a.getFloat(com.android.internal.R.styleable
.TextAppearance_shadowDx, 0.0f);
mShadowDy = a.getFloat(com.android.internal.R.styleable
.TextAppearance_shadowDy, 0.0f);
mShadowColor = a.getInt(com.android.internal.R.styleable
.TextAppearance_shadowColor, 0);
mHasElegantTextHeight = a.hasValue(com.android.internal.R.styleable
.TextAppearance_elegantTextHeight);
mElegantTextHeight = a.getBoolean(com.android.internal.R.styleable
.TextAppearance_elegantTextHeight, false);
mHasLetterSpacing = a.hasValue(com.android.internal.R.styleable
.TextAppearance_letterSpacing);
mLetterSpacing = a.getFloat(com.android.internal.R.styleable
.TextAppearance_letterSpacing, 0.0f);
mFontFeatureSettings = a.getString(com.android.internal.R.styleable
.TextAppearance_fontFeatureSettings);
mFontVariationSettings = a.getString(com.android.internal.R.styleable
.TextAppearance_fontVariationSettings);
a.recycle();
if (colorList >= 0) {
a = context.obtainStyledAttributes(com.android.internal.R.style.Theme,
com.android.internal.R.styleable.Theme);
textColor = a.getColorStateList(colorList);
a.recycle();
}
mTextColor = textColor;
}