Google is committed to advancing racial equity for Black communities. See how.

Builder

class Builder
kotlin.Any
   ↳ androidx.compose.ui.text.AnnotatedString.Builder

Builder class for AnnotatedString. Enables construction of an AnnotatedString using methods such as append and addStyle.

import androidx.compose.ui.text.SpanStyle

with(AnnotatedString.Builder("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()
}

Summary

Public constructors
<init>(text: String)

Create an Builder instance using the given String.

Create an Builder instance using the given AnnotatedString.

<init>(capacity: Int = 16)

Builder class for AnnotatedString.

Public methods
Unit
addStringAnnotation(tag: String, annotation: String, start: Int, end: Int)

Set an Annotation for the given range.

Unit
addStyle(style: SpanStyle, start: Int, end: Int)

Set a SpanStyle for the given range.

Unit
addStyle(style: ParagraphStyle, start: Int, end: Int)

Set a ParagraphStyle for the given range.

Unit
append(text: String)

Appends the given String to this Builder.

Unit
append(char: Char)

Appends the given Char to this Builder.

Unit

Appends the given AnnotatedString to this Builder.

Unit
pop()

Ends the style or annotation that was added via a push operation before.

Unit
pop(index: Int)

Ends the styles or annotation up to and including the pushStyle or pushStringAnnotation that returned the given index.

Int
pushStringAnnotation(tag: String, annotation: String)

Attach the given annotation to any appended text until a corresponding pop is called.

Int

Applies the given SpanStyle to any appended text until a corresponding pop is called.

Int

Applies the given ParagraphStyle to any appended text until a corresponding pop is called.

AnnotatedString

Constructs an AnnotatedString based on the configurations applied to the Builder.

String

Extension functions
From androidx.compose.foundation.text
Unit
AnnotatedString.Builder.appendInlineContent(id: String, alternateText: String = REPLACEMENT_CHAR)

Append an inline content into the AnnotatedString.

From androidx.compose.ui.text
R
AnnotatedString.Builder.withStyle(style: SpanStyle, crossinline block: AnnotatedString.Builder.() -> R)

Pushes style to the AnnotatedString.Builder, executes block and then pops the style.

R

Pushes style to the AnnotatedString.Builder, executes block and then pops the style.

Properties
Int

Returns the length of the String.

Public constructors

<init>

Builder(text: String)

Create an Builder instance using the given String.

<init>

Builder(text: AnnotatedString)

Create an Builder instance using the given AnnotatedString.

<init>

Builder(capacity: Int = 16)

Builder class for AnnotatedString. Enables construction of an AnnotatedString using methods such as append and addStyle.

import androidx.compose.ui.text.SpanStyle

with(AnnotatedString.Builder("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()
}
Parameters
capacity: Int = 16 initial capacity for the internal char buffer

Public methods

addStringAnnotation

fun addStringAnnotation(
    tag: String,
    annotation: String,
    start: Int,
    end: Int
): Unit

Set an Annotation for the given range.


with(AnnotatedString.Builder()) {
    append("link: Jetpack Compose")
    // attach a string annotation that stores a URL to the text "Jetpack Compose".
    addStringAnnotation(
        tag = "URL",
        annotation = "https://developer.android.com/jetpack/compose",
        start = 6,
        end = 21
    )
}
Parameters
tag: String the tag used to distinguish annotations
annotation: String the string annotation that is attached
start: Int the inclusive starting offset of the range
end: Int the exclusive end offset of the range

addStyle

fun addStyle(
    style: SpanStyle,
    start: Int,
    end: Int
): Unit

Set a SpanStyle for the given range.

Parameters
style: SpanStyle SpanStyle to be applied
start: Int the inclusive starting offset of the range
end: Int the exclusive end offset of the range

addStyle

fun addStyle(
    style: ParagraphStyle,
    start: Int,
    end: Int
): Unit

Set a ParagraphStyle for the given range. When a ParagraphStyle is applied to the AnnotatedString, it will be rendered as a separate paragraph.

Parameters
style: ParagraphStyle ParagraphStyle to be applied
start: Int the inclusive starting offset of the range
end: Int the exclusive end offset of the range

append

fun append(text: String): Unit

Appends the given String to this Builder.

Parameters
text: String the text to append

append

fun append(char: Char): Unit

Appends the given Char to this Builder.

Parameters
char: Char the Char to append

append

fun append(text: AnnotatedString): Unit

Appends the given AnnotatedString to this Builder.

Parameters
text: AnnotatedString the text to append

pop

fun pop(): Unit

Ends the style or annotation that was added via a push operation before.

pop

fun pop(index: Int): Unit

Ends the styles or annotation up to and including the pushStyle or pushStringAnnotation that returned the given index.

Parameters
index: Int the result of the a previous pushStyle or pushStringAnnotation in order to pop to

pushStringAnnotation

fun pushStringAnnotation(
    tag: String,
    annotation: String
): Int

Attach the given annotation to any appended text until a corresponding pop is called.


with(AnnotatedString.Builder()) {
    // push a string annotation to be applied to any appended text after this point.
    pushStringAnnotation("ParagrapLabel", "paragraph1")
    // append a paragraph, the annotation "paragraph1" is attached
    append("Paragraph One\n")
    // pop the annotation
    pop()
    // append new paragraph
    append("Paragraph Two\n")

    toAnnotatedString()
}
Parameters
tag: String the tag used to distinguish annotations
annotation: String the string annotation attached on this AnnotatedString

pushStyle

fun pushStyle(style: SpanStyle): Int

Applies the given SpanStyle to any appended text until a corresponding pop is called.

import androidx.compose.ui.text.SpanStyle

with(AnnotatedString.Builder()) {
    // push green text color so that any appended text will be rendered green
    pushStyle(SpanStyle(color = Color.Green))
    // append string, this text will be rendered green
    append("Hello")
    // pop the green text style
    pop()
    // append new string, this string will be default color
    append(" World")

    toAnnotatedString()
}
Parameters
style: SpanStyle SpanStyle to be applied

pushStyle

fun pushStyle(style: ParagraphStyle): Int

Applies the given ParagraphStyle to any appended text until a corresponding pop is called.

import androidx.compose.ui.text.ParagraphStyle

with(AnnotatedString.Builder()) {
    // push a ParagraphStyle to be applied to any appended text after this point.
    pushStyle(ParagraphStyle(lineHeight = 18.sp))
    // append a paragraph which will have lineHeight 18.sp
    append("Paragraph One\n")
    // pop the ParagraphStyle
    pop()
    // append new paragraph, this paragraph will not have the line height defined.
    append("Paragraph Two\n")

    toAnnotatedString()
}
Parameters
style: ParagraphStyle ParagraphStyle to be applied

toAnnotatedString

fun toAnnotatedString(): AnnotatedString

Constructs an AnnotatedString based on the configurations applied to the Builder.

toString

fun toString(): String

Properties

length

val length: Int

Returns the length of the String.