Is there a way to make a word bold or italics with...
# compose
d
Is there a way to make a word bold or italics within a Text in Jetpack Compose, without having to create different Texts for applying the bold or italics fontStyle?
n
you can use
AnnotatedString
.
Copy code
val builder = AnnotatedString.Builder().apply {
    append(
        AnnotatedString(
            "Texto Bold",
            SpanStyle(fontWeight = FontWeight.Bold)
        )
    )
    append(
        AnnotatedString(
            "Texto Strike",
            SpanStyle(textDecoration = TextDecoration.LineThrough)
        )
    )
    append(
        AnnotatedString(
            "Texto Big",
            SpanStyle(fontSize = 24.sp)
        )
    )
    append(
        AnnotatedString(
            "Texto Red",
            SpanStyle(color = Color.Red)
        )
    )
}
Text(text = builder.toAnnotatedString())
2
d
I was hoping it was possible to use simple markups like * or “<b></b>” for making a word bold within a text
h
There is
compose-richtext
[0] if you want something quick. It doesn't support markup/markdown rendering right now but would give you a good idea of how to accomplish something like that. Also there is a PR[1] for exactly doing "markdown rendering" using
compose-richtext
. [0] https://github.com/zach-klippenstein/compose-richtext [1] https://github.com/halilozercan/compose-richtext/pull/1
👍 1
z
That's not published as a library yet though unfortunately. Here's another solution: https://www.hellsoft.se/rendering-markdown-with-jetpack-compose/
667 Views