Skip to content

Instantly share code, notes, and snippets.

@hakobast
Created October 20, 2019 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakobast/5a8145e7bd07b5e4cd3588f3ca05ba16 to your computer and use it in GitHub Desktop.
Save hakobast/5a8145e7bd07b5e4cd3588f3ca05ba16 to your computer and use it in GitHub Desktop.
Extension function to replace spans
fun Spannable.replaceSpans(spanResolver: (index: Int, span: Any) -> Any?): Spannable {
this.getSpans(0, this.length, Any::class.java).forEachIndexed { index, span ->
val newSpan = spanResolver(index, span) ?: return@forEachIndexed
this.setSpan(
newSpan,
this.getSpanStart(span),
this.getSpanEnd(span),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
return this
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment