Google is committed to advancing racial equity for Black communities. See how.

AnnotatedString

@Immutable data class AnnotatedString
kotlin.Any
   ↳ androidx.ui.text.AnnotatedString

The basic data structure of text with multiple styles. To construct an AnnotatedString you can use Builder.

Summary

Nested classes

Builder class for AnnotatedString.

data

The information attached on the text such as a SpanStyle.

Public constructors
<init>(text: String, spanStyles: List<SpanStyleRange> = listOf(), paragraphStyles: List<ParagraphStyleRange> = listOf())

The basic data structure of text with multiple styles.

Public methods
List<StringAnnotation>
getStringAnnotations(tag: String, start: Int, end: Int)

Query the string annotations attached on this AnnotatedString.

List<StringAnnotation>
getStringAnnotations(start: Int, end: Int)

Query all of the string annotations attached on this AnnotatedString.

operator AnnotatedString

Extension functions
From androidx.ui.text
AnnotatedString
AnnotatedString.capitalize(localeList: LocaleList = LocaleList.current)

Create capitalized AnnotatedString

AnnotatedString
AnnotatedString.decapitalize(localeList: LocaleList = LocaleList.current)

Create capitalized AnnotatedString

AnnotatedString

Return a substring for the AnnotatedString and include the styles in the range of start (inclusive) and end (exclusive).

AnnotatedString
AnnotatedString.toLowerCase(localeList: LocaleList = LocaleList.current)

Create lower case transformed AnnotatedString

AnnotatedString
AnnotatedString.toUpperCase(localeList: LocaleList = LocaleList.current)

Create upper case transformed AnnotatedString

Properties
List<ParagraphStyleRange>

List<SpanStyleRange>

String

Extension properties
From androidx.ui.text
Int

Returns the length of the AnnotatedString.

Public constructors

<init>

AnnotatedString(
    text: String,
    spanStyles: List<SpanStyleRange> = listOf(),
    paragraphStyles: List<ParagraphStyleRange> = listOf())

The basic data structure of text with multiple styles. To construct an AnnotatedString you can use Builder.

import androidx.ui.text.AnnotatedString
import androidx.ui.text.ParagraphStyle
import androidx.ui.text.SpanStyle
import androidx.ui.text.style.TextIndent

AnnotatedString(
    text = "Hello World",
    // make "Hello" italic.
    spanStyles = listOf(
        AnnotatedString.Range(SpanStyle(fontStyle = FontStyle.Italic), 0, 5)
    ),
    // create two paragraphs with different alignment and indent settings.
    paragraphStyles = listOf(
        AnnotatedString.Range(ParagraphStyle(textAlign = TextAlign.Center), 0, 6),
        AnnotatedString.Range(ParagraphStyle(textIndent = TextIndent(5.sp)), 6, 11)
    )
)
Parameters
text: String the text to be displayed.
spanStyles: List<SpanStyleRange> = listOf() a list of Ranges that specifies SpanStyles on certain portion of the text. These styles will be applied in the order of the list. And the SpanStyles applied later can override the former styles. Notice that SpanStyle attributes which are null or androidx.ui.unit.TextUnit.Inherit won't change the current ones.
paragraphStyles: List<ParagraphStyleRange> = listOf() a list of Ranges that specifies ParagraphStyles on certain portion of the text. Each ParagraphStyle with a Range defines a paragraph of text. It's required that Ranges of paragraphs don't overlap with each other. If there are gaps between specified paragraph Ranges, a default paragraph will be created in between.
Exceptions
IllegalArgumentException if paragraphStyles contains any two overlapping Ranges.

Public methods

getStringAnnotations

fun getStringAnnotations(
    tag: String,
    start: Int,
    end: Int
): List<StringAnnotation>

Query the string annotations attached on this AnnotatedString. Annotations are metadata attached on the AnnotatedString, for example, a URL is a string metadata attached on the a certain range. Annotations are also store with Range like the styles.

Parameters
tag: String the tag of the annotations that is being queried. It's used to distinguish the annotations for different purposes.
start: Int the start of the query range, inclusive.
end: Int the end of the query range, exclusive.
Return
a list of annotations stored in Range. Notice that All annotations that intersect with the range [start, end) will be returned. When start is bigger than end, an empty list will be returned.

getStringAnnotations

fun getStringAnnotations(
    start: Int,
    end: Int
): List<StringAnnotation>

Query all of the string annotations attached on this AnnotatedString.

Parameters
start: Int the start of the query range, inclusive.
end: Int the end of the query range, exclusive.
Return
a list of annotations stored in Range. Notice that All annotations that intersect with the range [start, end) will be returned. When start is bigger than end, an empty list will be returned.

plus

@Stable operator fun plus(other: AnnotatedString): AnnotatedString

Properties

paragraphStyles

val paragraphStyles: List<ParagraphStyleRange>

spanStyles

val spanStyles: List<SpanStyleRange>

text

val text: String