Last active
December 14, 2016 16:10
-
-
Save joseph-zhong/2dd21e20d9d753544f09202913a57cee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SimpleSpanBuilder { | |
private class SpanSection{ | |
private final String text; | |
private final int startIndex; | |
private final ParcelableSpan[] spans; | |
public SpanSection(String text, int startIndex,ParcelableSpan... spans){ | |
this.spans = spans; | |
this.text = text; | |
this.startIndex = startIndex; | |
} | |
public void apply(SpannableStringBuilder spanStringBuilder){ | |
if (spanStringBuilder == null) return; | |
for (ParcelableSpan span : spans){ | |
spanStringBuilder.setSpan(span, 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,ParcelableSpan... spans){ | |
if (spans != null && spans.length > 0) { | |
spanSections.add(new SpanSection(text, stringBuilder.length(),spans)); | |
} | |
stringBuilder.append(text); | |
return this; | |
} | |
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment