public class Builder public constructor(capacity: Int = 16) : Appendable
Builds an AnnotatedString incrementally.
Implements Appendable for compatibility with standard text APIs.
Parameters
| capacity | initial capacity for the internal buffer |
Properties
length
public val length: Int
Returns the length of the String.
Functions
append
public fun append(text: String)
Appends the given String to this Builder.
Parameters
| text | the text to append |
deprecatedappendreturning_void
@Deprecated(
message =
"Replaced by the append(Char) method that returns an Appendable. " +
"This method must be kept around for binary compatibility.",
level = DeprecationLevel.HIDDEN,
)
// Set the JvmName to preserve compatibility with bytecode that expects a void return type.
public fun deprecated_append_returning_void(char: Char)
append
public fun append(text: AnnotatedString)
Appends the given AnnotatedString to this Builder.
Parameters
| text | the text to append |
append
public fun append(text: AnnotatedString, start: Int, end: Int)
Appends the range of text between start (inclusive) and end (exclusive) to this Builder. All spans and annotations from text between start and end will be copied over as well.
Parameters
| text | the text to append |
| start | The index of the first character in text to copy over (inclusive). |
| end | The index after the last character in text to copy over (exclusive). |
addStyle
public fun addStyle(style: SpanStyle, start: Int, end: Int)
Applies style to the given range.
Parameters
| style | SpanStyle to apply |
| start | inclusive start offset |
| end | exclusive end offset |
addStyle
public fun addStyle(style: ParagraphStyle, start: Int, end: Int)
Applies style to the given range, creating a separate paragraph.
Paragraph ranges must follow paragraph arrangement rules. See AnnotatedString class documentation for details and examples.
Parameters
| style | ParagraphStyle to apply |
| start | inclusive start offset |
| end | exclusive end offset |
addStringAnnotation
public fun addStringAnnotation(tag: String, annotation: String, start: Int, end: Int)
Associates a string annotation with a range.
Parameters
| tag | tag to identify the annotation |
| annotation | string annotation value |
| start | inclusive start offset |
| end | exclusive end offset |
addTtsAnnotation
public fun addTtsAnnotation(ttsAnnotation: TtsAnnotation, start: Int, end: Int)
Set a TtsAnnotation for the given range defined by start and end.
Parameters
| ttsAnnotation | an object that stores text to speech metadata that intended for the TTS engine. |
| start | the inclusive starting offset of the range |
| end | the exclusive end offset of the range |
addUrlAnnotation
@Deprecated(
"Use LinkAnnotation API for links instead",
ReplaceWith("addLink(, start, end)"),
)
public fun addUrlAnnotation(urlAnnotation: UrlAnnotation, start: Int, end: Int)
Set a UrlAnnotation for the given range defined by start and end. URLs may be treated specially by screen readers, including being identified while reading text with an audio icon or being summarized in a links menu.
Parameters
| urlAnnotation | A UrlAnnotation object that stores the URL being linked to. |
| start | the inclusive starting offset of the range |
| end | the exclusive end offset of the range |
addLink
public fun addLink(url: LinkAnnotation.Url, start: Int, end: Int)
Associates a URL link with a range.
Clicking the text opens the URL using androidx.compose.ui.platform.UriHandler.
Screen readers present URLs in different ways, such as using a links menu or audio cues.
Parameters
| url | the target URL |
| start | inclusive start offset |
| end | exclusive end offset |
addLink
public fun addLink(clickable: LinkAnnotation.Clickable, start: Int, end: Int)
Associates a clickable link with a range.
Clicking the text triggers a LinkInteractionListener with clickable.
Screen readers present clickables in different ways, such as using a links menu or audio cues.
Parameters
| clickable | click metadata |
| start | inclusive start offset |
| end | exclusive end offset |
addBullet
public fun addBullet(bullet: Bullet, start: Int, end: Int)
Adds an annotation to draw a bullet. Unlike another overload, this one doesn't add a separate ParagraphStyle. As so for bullet to be rendered, make sure it starts on a separate line by adding a newline before or wrapping with a ParagraphStyle.
For a convenient API to create a bullet list check withBulletList.
Parameters
| bullet | a bullet to draw before the text |
| start | the inclusive starting offset of the range |
| end | the exclusive end offset of the range |
addBullet
public fun addBullet(bullet: Bullet, indentation: TextUnit, start: Int, end: Int)
Adds an annotation to draw a bullet together with a paragraph that adds an indentation.
Parameters
| bullet | a bullet to draw before the text |
| indentation | indentation that is added to the paragraph. Note that this indentation should be large enough to fit a bullet and a padding between the bullet and beginning of the paragraph |
| start | the inclusive starting offset of the range |
| end | the exclusive end offset of the range |
pushStyle
public fun pushStyle(style: SpanStyle): Int
Applies the given SpanStyle to any appended text until a corresponding pop is called.
Parameters
| style | SpanStyle to be applied |
pushStyle
public fun pushStyle(style: ParagraphStyle): Int
Applies the given ParagraphStyle to any appended text until a corresponding pop is called.
Parameters
| style | ParagraphStyle to be applied |
pushBullet
public fun pushBullet(bullet: Bullet): Int
Applies the given bullet annotation to any appended text until a corresponding pop is called. For bullet to be rendered, make sure it starts on a separate line by either adding a newline before or by wrapping with a ParagraphStyle.
For a convenient API to create a bullet list check withBulletList.
withBulletList
public fun <R : Any> withBulletList(
indentation: TextUnit = Bullet.DefaultIndentation,
bullet: Bullet = Bullet.Default,
block: BulletScope.() -> R,
): R
Creates a bullet list which allows to define a common indentation and a bullet for evey bullet list item created inside the list.
Note that when nesting the withBulletList calls, the indentation inside the nested list will be a combination of all indentations in the nested chain. For example,
withBulletList(10.sp) {
withBulletList(15.sp) {
// items indentation 25.sp
}
}
withBulletListItem
public fun <R : Any> BulletScope.withBulletListItem(
bullet: Bullet? = null,
block: Builder.() -> R,
): R
Creates a bullet list item around the content produced by the block. The list item creates a separate paragraph with the indentation to the bullet defined by the preceding Builder.withBulletList calls.
Parameters
| bullet | defines the bullet to be drawn |
| block | function to be executed |
pushStringAnnotation
public fun pushStringAnnotation(tag: String, annotation: String): Int
Attach the given annotation to any appended text until a corresponding pop is called.
Parameters
| tag | the tag used to distinguish annotations |
| annotation | the string annotation attached on this AnnotatedString |
pushTtsAnnotation
public fun pushTtsAnnotation(ttsAnnotation: TtsAnnotation): Int
Attach the given ttsAnnotation to any appended text until a corresponding pop is called.
Parameters
| ttsAnnotation | an object that stores text to speech metadata that intended for the TTS engine. |
pushUrlAnnotation
@Deprecated(
"Use LinkAnnotation API for links instead",
ReplaceWith("pushLink(, start, end)"),
)
public fun pushUrlAnnotation(urlAnnotation: UrlAnnotation): Int
Attach the given UrlAnnotation to any appended text until a corresponding pop is called.
Parameters
| urlAnnotation | A UrlAnnotation object that stores the URL being linked to. |
pushLink
public fun pushLink(link: LinkAnnotation): Int
Attach the given LinkAnnotation to any appended text until a corresponding pop is called.
Parameters
| link | A LinkAnnotation object that stores the URL or clickable tag being linked to. |
pop
public fun pop()
Ends the style or annotation that was added via a push operation before.
pop
public fun pop(index: Int)
Ends the styles or annotation up to and including the pushStyle or pushStringAnnotation that returned the given index.
Parameters
| index | the result of the a previous pushStyle or pushStringAnnotation in order to pop to |
toAnnotatedString
public fun toAnnotatedString(): AnnotatedString
Constructs an AnnotatedString based on the configurations applied to the Builder.
Members
BulletScope
public class BulletScope internal constructor(internal val builder: Builder)
Scope for a bullet list