Skip to content

Instantly share code, notes, and snippets.

@glureau
Last active June 4, 2019 15:46
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 glureau/f56821d26c1cfe095e305d708ac9bc62 to your computer and use it in GitHub Desktop.
Save glureau/f56821d26c1cfe095e305d708ac9bc62 to your computer and use it in GitHub Desktop.
class MediumActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_medium)
tv3.text = "FOR <picto> DEVS"
val drawable = ContextCompat.getDrawable(this, R.drawable.medium_logo_lowres)!!.apply {
setBounds(0, 0, intrinsicWidth, intrinsicHeight)
}
tv3.replaceTagWithDrawable("<picto>", { drawable })
}
private fun TextView.replaceTagWithDrawable(tag: String, drawableFactory: () -> Drawable, ignoreCase: Boolean = true) {
val tagPos = text.indexOf(tag, ignoreCase = ignoreCase)
if (tagPos >= 0) {
var spannableString = text as? SpannableString?
if (spannableString == null) {
spannableString = SpannableString(text)
}
spannableString.setSpan(
ImageSpan(drawableFactory(), ImageSpan.ALIGN_BASELINE),
tagPos,
tagPos + tag.length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
text = spannableString
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment