AnnotatedString.Companion



Summary

Public properties

Saver<AnnotatedString, *>

The default Saver implementation for AnnotatedString.

Cmn

Extension functions

AnnotatedString
AnnotatedString.Companion.fromHtml(
    htmlString: String,
    linkStyle: SpanStyle?,
    linkFocusedStyle: SpanStyle?,
    linkHoveredStyle: SpanStyle?,
    linkPressedStyle: SpanStyle?,
    linkInteractionListener: LinkInteractionListener?
)

Converts a string with HTML tags into AnnotatedString.

Cmn
android

Public properties

Saver

val SaverSaver<AnnotatedString, *>

The default Saver implementation for AnnotatedString.

Note this Saver doesn't preserve the LinkInteractionListener of the links. You should handle this case manually if required (check https://issuetracker.google.com/issues/332901550 for an example).

Extension functions

fromHtml

fun AnnotatedString.Companion.fromHtml(
    htmlString: String,
    linkStyle: SpanStyle? = SpanStyle(textDecoration = TextDecoration.Underline),
    linkFocusedStyle: SpanStyle? = null,
    linkHoveredStyle: SpanStyle? = null,
    linkPressedStyle: SpanStyle? = null,
    linkInteractionListener: LinkInteractionListener? = null
): AnnotatedString

Converts a string with HTML tags into 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 guide. Note that bullet lists are not yet available.

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: String

HTML-tagged string to be parsed to construct AnnotatedString

linkStyle: SpanStyle? = SpanStyle(textDecoration = TextDecoration.Underline)

style to be applied to links present in the string

linkFocusedStyle: SpanStyle? = null

style to be applied to links present in the string when they are focused

linkHoveredStyle: SpanStyle? = null

style to be applied to links present in the string when they are hovered

linkPressedStyle: SpanStyle? = null

style to be applied to links present in the string when they are pressed

linkInteractionListener: 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 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 linkStyle, the link will be displayed as green. If, however, you pass a green background via the linkStyle instead, the link will be displayed as red on a green background.

Example of displaying styled string from resources

See also
LinkAnnotation