Skip to content

Instantly share code, notes, and snippets.

@ataulm
Last active March 29, 2018 21:32
Show Gist options
  • Save ataulm/19bbbf0a838b746c6a40d0bb0314a053 to your computer and use it in GitHub Desktop.
Save ataulm/19bbbf0a838b746c6a40d0bb0314a053 to your computer and use it in GitHub Desktop.
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