我知道这是对已经回答的问题的新回复,但我想分享一个我制作的实用类,它可以使这个任务更加容易。
Java版本
public class SimpleSpanBuilder {
private class SpanSection{
private final String text;
private final int startIndex;
private final CharacterStyle[] styles;
private SpanSection(String text, int startIndex,CharacterStyle... styles){
this.styles = styles;
this.text = text;
this.startIndex = startIndex;
}
private void apply(SpannableStringBuilder spanStringBuilder){
if (spanStringBuilder == null) return;
for (CharacterStyle style : styles){
spanStringBuilder.setSpan(style, startIndex, startIndex + text.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
}
private List<SpanSection> spanSections;
private StringBuilder stringBuilder;
public SimpleSpanBuilder(){
stringBuilder = new StringBuilder();
spanSections = new ArrayList<>();
}
public SimpleSpanBuilder append(String text,CharacterStyle... styles){
if (styles != null && styles.length > 0) {
spanSections.add(new SpanSection(text, stringBuilder.length(),styles));
}
stringBuilder.append(text);
return this;
}
public SimpleSpanBuilder appendWithSpace(String text,CharacterStyle... styles){
return append(text.concat(" "),styles);
}
public SimpleSpanBuilder appendWithLineBreak(String text,CharacterStyle... styles){
return append(text.concat("\n"),styles);
}
public SpannableStringBuilder build(){
SpannableStringBuilder ssb = new SpannableStringBuilder(stringBuilder.toString());
for (SpanSection section : spanSections){
section.apply(ssb);
}
return ssb;
}
@Override
public String toString() {
return stringBuilder.toString();
}
}
使用方法:
SimpleSpanBuilder ssb = new SimpleSpanBuilder();
ssb.appendWithSpace("Hello");
ssb.append("StackOverflow",new ForegroundColorSpan(Color.RED),new RelativeSizeSpan(1.5));
textView.setText(ssb.build());
Kotlin 版本
class SimpleSpanBuilder() {
class Span {
private var startIndex: Int = 0
internal var text: String
private var styles: Array<out CharacterStyle>
internal constructor(index: Int, text: String, vararg styles: CharacterStyle) {
this.startIndex = index
this.text = text
this.styles = styles
}
constructor(text: String, vararg styles: CharacterStyle) : this(0, text, *styles)
internal fun setIndex(index: Int): Span {
return Span(index, this.text, *this.styles)
}
internal fun apply(spanStringBuilder: SpannableStringBuilder?) {
if (spanStringBuilder == null) return
for (style in styles) {
spanStringBuilder.setSpan(
style,
startIndex,
startIndex + text.length,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE
)
}
}
}
private val spanSections = mutableListOf<Span>()
private val stringBuilder = StringBuilder()
constructor(text: String, vararg styles: CharacterStyle) : this() {
plus(Span(text, *styles))
}
operator fun plus(span: SimpleSpanBuilder.Span): SimpleSpanBuilder {
spanSections.add(span.setIndex(stringBuilder.length))
stringBuilder.append(span.text)
return this
}
fun build(): SpannableStringBuilder {
val ssb = SpannableStringBuilder(stringBuilder.toString())
for (section in spanSections) {
section.apply(ssb)
}
return ssb
}
override fun toString(): String {
return stringBuilder.toString()
}
}
使用方法
var ssb = SimpleSpanBuilder("Hello ",ForegroundColorSpan(Color.BLUE))
ssb += SimpleSpanBuilder.Span(
"StackOverflow",
ForegroundColorSpan(Color.RED),
RelativeSizeSpan(1.5f)
)
textView.text = ssb.build()