Builder

class Builder
kotlin.Any
   ↳ androidx.ui.text.AnnotatedString.Builder

Builder class for AnnotatedString. Enables construction of an AnnotatedString using methods such as append and addStyle.

import androidx.ui.text.TextStyle

with(AnnotatedString.Builder("Hello")) {
    // push green text style so that any appended text will be green
    pushStyle(TextStyle(color = Color.Green))
    // append new text, this text will be rendered as green
    append(" World")
    // pop the green style
    popStyle()
    // append a string without style
    append("!")
    // then style the last added word as red, exclamation mark will be red
    addStyle(TextStyle(color = Color.Red), "Hello World".length, this.length)

    toAnnotatedString()
}

Summary

Public constructors

<init>(text: String)

Create an Builder instance using the given String.

Create an Builder instance using the given AnnotatedString.

<init>(capacity: Int = 16)

Builder class for AnnotatedString.

Public methods

Unit
addStyle(style: TextStyle, start: Int, end: Int)

Set a TextStyle for the given range.

Unit
addStyle(style: ParagraphStyle, start: Int, end: Int)

Set a ParagraphStyle for the given range.

Unit
append(text: String)

Appends the given String to this Builder.

Unit
append(char: Char)

Appends the given Char to this Builder.

Unit

Appends the given AnnotatedString to this Builder.

Unit

Ends the style that was added via a push operation before.

Unit
popStyle(index: Int)

Ends the styles up to and including the pushStyle that returned the given index.

Int

Applies the given TextStyle to any appended text until a corresponding popStyle is called.

Int

Applies the given ParagraphStyle to any appended text until a corresponding popStyle is called.

AnnotatedString

Constructs an AnnotatedString based on the configurations applied to the Builder.

String

Extension functions

From androidx.ui.text
R
AnnotatedString.Builder.withStyle(style: TextStyle, crossinline block: AnnotatedString.Builder.() -> R)

Pushes style to the AnnotatedString.Builder, executes block and then pops the style.

R

Pushes style to the AnnotatedString.Builder, executes block and then pops the style.

Properties

Int

Returns the length of the String.

Public constructors

<init>

Builder(text: String)

Create an Builder instance using the given String.

<init>

Builder(text: AnnotatedString)

Create an Builder instance using the given AnnotatedString.

<init>

Builder(capacity: Int = 16)

Builder class for AnnotatedString. Enables construction of an AnnotatedString using methods such as append and addStyle.

import androidx.ui.text.TextStyle

with(AnnotatedString.Builder("Hello")) {
    // push green text style so that any appended text will be green
    pushStyle(TextStyle(color = Color.Green))
    // append new text, this text will be rendered as green
    append(" World")
    // pop the green style
    popStyle()
    // append a string without style
    append("!")
    // then style the last added word as red, exclamation mark will be red
    addStyle(TextStyle(color = Color.Red), "Hello World".length, this.length)

    toAnnotatedString()
}
Parameters
capacity: Int = 16 initial capacity for the internal char buffer

Public methods

addStyle

fun addStyle(
    style: TextStyle,
    start: Int,
    end: Int
): Unit

Set a TextStyle for the given range.

Parameters
style: TextStyle TextStyle to be applied
start: Int the inclusive starting offset of the range
end: Int the exclusive end offset of the range

addStyle

fun addStyle(
    style: ParagraphStyle,
    start: Int,
    end: Int
): Unit

Set a ParagraphStyle for the given range. When a ParagraphStyle is applied to the AnnotatedString, it will be rendered as a separate paragraph.

Parameters
style: ParagraphStyle ParagraphStyle to be applied
start: Int the inclusive starting offset of the range
end: Int the exclusive end offset of the range

append

fun append(text: String): Unit

Appends the given String to this Builder.

Parameters
text: String the text to append

append

fun append(char: Char): Unit

Appends the given Char to this Builder.

Parameters
char: Char the Char to append

append

fun append(text: AnnotatedString): Unit

Appends the given AnnotatedString to this Builder.

Parameters
text: AnnotatedString the text to append

popStyle

fun popStyle(): Unit

Ends the style that was added via a push operation before.

See Also

popStyle

fun popStyle(index: Int): Unit

Ends the styles up to and including the pushStyle that returned the given index.

Parameters
index: Int the result of the a previous pushStyle in order to pop to

See Also

pushStyle

fun pushStyle(style: TextStyle): Int

Applies the given TextStyle to any appended text until a corresponding popStyle is called.

import androidx.ui.text.TextStyle

with(AnnotatedString.Builder()) {
    // push green text color so that any appended text will be rendered green
    pushStyle(TextStyle(color = Color.Green))
    // append string, this text will be rendered green
    append("Hello")
    // pop the green text style
    popStyle()
    // append new string, this string will be default color
    append(" World")

    toAnnotatedString()
}
Parameters
style: TextStyle TextStyle to be applied

pushStyle

fun pushStyle(style: ParagraphStyle): Int

Applies the given ParagraphStyle to any appended text until a corresponding popStyle is called.

import androidx.ui.text.ParagraphStyle

with(AnnotatedString.Builder()) {
    // push a ParagraphStyle to be applied to any appended text after this point.
    pushStyle(ParagraphStyle(lineHeight = 18.sp))
    // append a paragraph which will have lineHeight 18.sp
    append("Paragraph One\n")
    // pop the ParagraphStyle
    popStyle()
    // append new paragraph, this paragraph will not have the line height defined.
    append("Paragraph Two\n")

    toAnnotatedString()
}
Parameters
style: ParagraphStyle ParagraphStyle to be applied

toAnnotatedString

fun toAnnotatedString(): AnnotatedString

Constructs an AnnotatedString based on the configurations applied to the Builder.

toString

fun toString(): String

Properties

length

val length: Int

Returns the length of the String.