# AnnotatedString.Annotation

Artifact: [androidx.compose.ui:ui-text](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/text/AnnotatedString.kt+class:androidx.compose.ui.text.AnnotatedString.Annotation) Added in [1.8.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.8.0) Common/AllAndroid/JVM
- Cmn

  ```
  sealed interface AnnotatedString.Annotation
  ```
Known direct subclasses [Bullet](https://developer.android.com/reference/kotlin/androidx/compose/ui/text/Bullet), [LinkAnnotation](https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation), [ParagraphStyle](https://developer.android.com/reference/kotlin/androidx/compose/ui/text/ParagraphStyle), [SpanStyle](https://developer.android.com/reference/kotlin/androidx/compose/ui/text/SpanStyle), [StringAnnotation](https://developer.android.com/reference/kotlin/androidx/compose/ui/text/StringAnnotation), [TtsAnnotation](https://developer.android.com/reference/kotlin/androidx/compose/ui/text/TtsAnnotation), [UrlAnnotation](https://developer.android.com/reference/kotlin/androidx/compose/ui/text/UrlAnnotation)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/Bullet` | Draws a bullet point next to a paragraph. |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation` | An annotation that represents a clickable part of the text. |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/ParagraphStyle` | Paragraph styling configuration. |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/SpanStyle` | Styling configuration that applies at the character level. |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/StringAnnotation` | An `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/AnnotatedString.Annotation` class which holds a String `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/StringAnnotation#value()`. |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/TtsAnnotation` | An annotation that contains the metadata intended for text-to-speech engine. |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/UrlAnnotation` | **This class is deprecated.** Use LinkAnnotatation.Url(url) instead |

Known indirect subclasses [LinkAnnotation.Clickable](https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation.Clickable), [LinkAnnotation.Url](https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation.Url), [VerbatimTtsAnnotation](https://developer.android.com/reference/kotlin/androidx/compose/ui/text/VerbatimTtsAnnotation)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation.Clickable` | An annotation that contains a clickable marked with `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation.Clickable#tag()`. |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation.Url` | An annotation that contains a `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation.Url#url()` string. |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/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

- `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/SpanStyle` specifies character level styling such as color, font, letter spacing etc.

- `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/ParagraphStyle` for configuring styling on a paragraph level such as line heights, text aligning, text direction etc.

- `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation` to mark links in the text.

- `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/TtsAnnotation` provides information to assistive technologies such as screen readers.

- Custom annotations using the `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/StringAnnotation`.

## Summary

| ### Public companion properties |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver<https://developer.android.com/reference/kotlin/androidx/compose/ui/text/AnnotatedString.Annotation, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html>` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/AnnotatedString.Annotation#Saver()` Saves and restores `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/AnnotatedString.Annotation` objects. | Cmn |

## Public companion properties

### Saver

Cmn Added in [1.13.0-rc01](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.13.0-rc01)

```
val Saver: Saver<AnnotatedString.Annotation, Any>
```

Saves and restores `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/AnnotatedString.Annotation` objects.

Supports the following annotation types:

- `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/ParagraphStyle`

- `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/SpanStyle`

- `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/VerbatimTtsAnnotation`

- `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/UrlAnnotation`

- `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation.Url`

- `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation.Clickable`

- `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/StringAnnotation`

Note: Does not preserve `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkInteractionListener` of `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation`s, and `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/Bullet` annotations are not preserved at the moment. Handle saving and restoring them manually if required.

```kotlin
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) }
```

```kotlin
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!!)
```