Skip to content

Instantly share code, notes, and snippets.

@ataulm
Last active Mar 29, 2018
Embed
What would you like to do?
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_my)
story.text = writeFormattedStory()
}
private fun writeFormattedStory(): CharSequence {
return SpannableStringBuilder()
.append(createWithSpans("A Short Story", StyleSpan(Typeface.BOLD)))
.append("\n")
.append("This is a short story")
.append("\n")
.append(createWithSpans("fin.", StyleSpan(Typeface.ITALIC)))
}
private fun createWithSpans(charSequence: CharSequence, vararg spans: Any): SpannableString {
val spannableString = SpannableString(charSequence)
for (span in spans) {
spannableString.setSpan(span, 0, charSequence.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
return spannableString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment