# AnnotatedString.Companion

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.Companion) Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.7.0) Common/AllAndroid/JVM
- Cmn

  ```
  object AnnotatedString.Companion
  ```

*** ** * ** ***

## Summary

| ### Public properties |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver<https://developer.android.com/reference/kotlin/androidx/compose/ui/text/AnnotatedString, *>` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/AnnotatedString#Saver()` The default `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/AnnotatedString#Saver()` implementation for `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/AnnotatedString`. | Cmn |

| ### Extension functions |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/AnnotatedString` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/AnnotatedString.Companion.https://developer.android.com/reference/kotlin/androidx/compose/ui/text/AnnotatedString.Companion#(androidx.compose.ui.text.AnnotatedString.Companion).fromHtml(kotlin.String,androidx.compose.ui.text.TextLinkStyles,androidx.compose.ui.text.LinkInteractionListener)( htmlString: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, linkStyles: https://developer.android.com/reference/kotlin/androidx/compose/ui/text/TextLinkStyles?, linkInteractionListener: https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkInteractionListener? )` Converts a string with HTML tags into `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/AnnotatedString`. | android |

## Public properties

### Saver

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

```
val Saver: Saver<AnnotatedString, *>
```

The default `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/AnnotatedString#Saver()` implementation for `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/AnnotatedString`.

Note this Saver doesn't preserve the `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkInteractionListener` of the links. You should handle this case manually if required (check https://issuetracker.google.com/issues/332901550 for an example).

## Extension functions

### AnnotatedString.Companion.fromHtml

android 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/Html.android.kt+function:fromHtml)

```
fun AnnotatedString.Companion.fromHtml(
    htmlString: String,
    linkStyles: TextLinkStyles? = null,
    linkInteractionListener: LinkInteractionListener? = null
): AnnotatedString
```

Converts a string with HTML tags into `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/AnnotatedString`.

If you define your string in the resources, make sure to use HTML-escaped opening brackets `<` instead of `<`.

For a list of supported tags go check [Styling with HTML markup](https://developer.android.com/guide/topics/resources/string-resource#StylingWithHTML) guide.

To support nested bullet list, the nested sub-list wrapped in

tag MUST be placed inside a list item with a tag
- . In other words, you must add wrapped sub-list in between opening and closing
- tag of the wrapping sub-list. This is due to the specificities of the underlying XML/HTML parser.<br />

  ```kotlin
  import androidx.compose.foundation.text.BasicText
  import androidx.compose.ui.res.stringResource
  import androidx.compose.ui.text.AnnotatedString
  import androidx.compose.ui.text.fromHtml

  // First, download a string as a plain text using one of the resources' methods. At this stage
  // you will be handling plurals and formatted strings in needed. Moreover, the string will be
  // resolved with respect to the current locale and available translations.
  val string = stringResource(id = R.string.example)

  // Next, convert a string marked with HTML tags into AnnotatedString to be displayed by Text
  val styledAnnotatedString = AnnotatedString.fromHtml(htmlString = string)

  BasicText(styledAnnotatedString)
  ```

  | Parameters |
  |---|---|
  | `htmlString: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | HTML-tagged string to be parsed to construct AnnotatedString |
  | `linkStyles: https://developer.android.com/reference/kotlin/androidx/compose/ui/text/TextLinkStyles? = null` | style configuration to be applied to links present in the string in different styles |
  | `linkInteractionListener: https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkInteractionListener? = null` | a listener that will be attached to links that are present in the string and triggered when user clicks on those links. When set to null, which is a default, the system will try to open the corresponding links with the `https://developer.android.com/reference/kotlin/androidx/compose/ui/platform/UriHandler` composition local Note that any link style passed directly to this method will be merged with the styles set directly on a HTML-tagged string. For example, if you set a color of the link via the span annotation to "red" but also pass a green color via the `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/package-summary#(androidx.compose.ui.text.AnnotatedString.Companion).fromHtml(kotlin.String,androidx.compose.ui.text.TextLinkStyles,androidx.compose.ui.text.LinkInteractionListener)`, the link will be displayed as green. If, however, you pass a green background via the `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/package-summary#(androidx.compose.ui.text.AnnotatedString.Companion).fromHtml(kotlin.String,androidx.compose.ui.text.TextLinkStyles,androidx.compose.ui.text.LinkInteractionListener)` instead, the link will be displayed as red on a green background. Example of displaying styled string from resources |

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