Created
June 8, 2018 13:02
-
-
Save rsicarelli/91413e50ac9fed826129867816a485b0 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
class SpannableTextView @JvmOverloads constructor( | |
context: Context, | |
attrs: AttributeSet? = null, | |
defStyle: Int = 0 | |
) : TextView(context, attrs, defStyle) { | |
fun setSpannableText(vararg texts: SpannableStyle) { | |
buildWith(breaker = " ", blocks = *texts) | |
} | |
private fun buildWith(breaker: String, vararg blocks: SpannableStyle) { | |
val builder = SpannableStringBuilder() | |
for (item in blocks) { | |
builder.apply { | |
append(convertToSpannableString(item)).append(breaker) | |
} | |
} | |
text = builder.subSequence(0, builder.length).trim() | |
} | |
private fun convertToSpannableString(spannableStyle: SpannableStyle): SpannableString { | |
return SpannableString(spannableStyle.text).apply { | |
setSpan(TextAppearanceSpan(context, spannableStyle.style), 0, spannableStyle.text.length, 0) | |
} | |
} | |
class SpannableStyle(val text: String, val style: Int) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment