AnnotatedString.Companion
-
Cmn
object AnnotatedString.Companion
Summary
Public properties |
||
|---|---|---|
Saver<AnnotatedString, *> |
The default |
Cmn
|
Extension functions |
||
|---|---|---|
AnnotatedString |
AnnotatedString.Companion.fromHtml(Converts a string with HTML tags into |
android
|
Public properties
Saver
val Saver: Saver<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,
linkStyles: TextLinkStyles? = 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.
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.
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: StringHTML-tagged string to be parsed to construct AnnotatedString
linkStyles: TextLinkStyles? = nullstyle configuration to be applied to links present in the string in different styles
linkInteractionListener: LinkInteractionListener? = nulla 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.UriHandlercomposition localNote 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
linkStyles, the link will be displayed as green. If, however, you pass a green background via thelinkStylesinstead, the link will be displayed as red on a green background.Example of displaying styled string from resources
See also LinkAnnotation