Created
October 20, 2019 10:32
-
-
Save hakobast/5a8145e7bd07b5e4cd3588f3ca05ba16 to your computer and use it in GitHub Desktop.
Extension function to replace spans
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
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