AnnotatedString.Annotation

Known direct subclasses
Bullet

Draws a bullet point next to a paragraph.

LinkAnnotation

An annotation that represents a clickable part of the text.

ParagraphStyle

Paragraph styling configuration.

SpanStyle

Styling configuration that applies at the character level.

StringAnnotation

An AnnotatedString.Annotation class which holds a String value.

TtsAnnotation

An annotation that contains the metadata intended for text-to-speech engine.

UrlAnnotation

This class is deprecated. Use LinkAnnotatation.Url(url) instead

Known indirect subclasses
LinkAnnotation.Clickable

An annotation that contains a clickable marked with tag.

LinkAnnotation.Url

An annotation that contains a url string.

VerbatimTtsAnnotation

The text associated with this annotation is a series of characters that have to be read verbatim.


Defines annotations that specify additional information to apply to ranges of text within the given AnnotatedString.

The AnnotatedString supports annotations that provide different kind of information, such as

  • SpanStyle specifies character level styling such as color, font, letter spacing etc.

  • ParagraphStyle for configuring styling on a paragraph level such as line heights, text aligning, text direction etc.

  • LinkAnnotation to mark links in the text.

  • TtsAnnotation provides information to assistive technologies such as screen readers.

  • Custom annotations using the StringAnnotation.

Summary

Public companion properties

Saver<AnnotatedString.AnnotationAny>

Saves and restores AnnotatedString.Annotation objects.

Cmn

Public companion properties

Saver

val SaverSaver<AnnotatedString.AnnotationAny>

Saves and restores AnnotatedString.Annotation objects.

Supports the following annotation types:

Note: Does not preserve LinkInteractionListener of LinkAnnotations, and Bullet annotations are not preserved at the moment. Handle saving and restoring them manually if required.

import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.SaverScope
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.LinkAnnotation
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextLinkStyles

// Demonstrates how to save and restore a single Annotation (such as a LinkAnnotation)
// using `AnnotatedString.Annotation.Saver`.
val annotation: AnnotatedString.Annotation =
    LinkAnnotation.Url(
        url = "https://developer.android.com",
        styles = TextLinkStyles(SpanStyle(color = Color.Blue)),
    )

val saverScope = SaverScope { true }

// Save the annotation
val saved = with(AnnotatedString.Annotation.Saver) { saverScope.save(annotation) }

// Restore the annotation
val restored = saved?.let { AnnotatedString.Annotation.Saver.restore(it) }
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.SaverScope
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.LinkAnnotation
import androidx.compose.ui.text.LinkInteractionListener
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextLinkStyles

// Standard `AnnotatedString.Annotation.Saver` restores LinkAnnotation with
// `linkInteractionListener = null`
// because callbacks cannot be saved into a Bundle across process death.
// This sample demonstrates how to create a custom Saver for LinkAnnotation.Url that delegates
// saving to `AnnotatedString.Annotation.Saver` and re-attaches a listener upon restoration.
val myListener = LinkInteractionListener {
    // Handle link click interaction
}

val customLinkSaver =
    Saver<LinkAnnotation.Url, Any>(
        save = { link ->
            // Delegate saving of url and styles to Annotation.Saver
            with(AnnotatedString.Annotation.Saver) { save(link) }
        },
        restore = { value ->
            // Restore the base LinkAnnotation.Url and re-attach the listener
            val baseLink =
                with(AnnotatedString.Annotation.Saver) { restore(value) } as? LinkAnnotation.Url
            baseLink?.copy(linkInteractionListener = myListener)
        },
    )

val originalLink =
    LinkAnnotation.Url(
        url = "https://developer.android.com",
        styles = TextLinkStyles(SpanStyle(color = Color.Blue)),
        linkInteractionListener = myListener,
    )

val saverScope = SaverScope { true }
val saved = with(customLinkSaver) { saverScope.save(originalLink) }
val restored = customLinkSaver.restore(saved!!)