Class

AnnotatedString.Builder

Builds an AnnotatedString incrementally.

AnnotatedStringBuilderSample

@Sampled
fun AnnotatedStringBuilderSample() {
    buildAnnotatedString {
        append("Hello")
        // push green text style so that any appended text will be green
        pushStyle(SpanStyle(color = Color.Green))
        // append new text, this text will be rendered as green
        append(" World")
        // pop the green style
        pop()
        // append a string without style
        append("!")
        // then style the last added word as red, exclamation mark will be red
        addStyle(SpanStyle(color = Color.Red), "Hello World".length, this.length)

        toAnnotatedString()
    }
}

AnnotatedStringBuilderAppendableSample

@Sampled
fun AnnotatedStringBuilderAppendableSample() {
    val words = listOf("Hello", "World")
    buildAnnotatedString {
        // joinTo is a Kotlin stdlib function that takes an Appendable
        words.joinTo(this, separator = " ", postfix = "!")

        toAnnotatedString()
    }
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: