Last active
June 4, 2019 15:46
-
-
Save glureau/f56821d26c1cfe095e305d708ac9bc62 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 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