TextFieldValue

Class

Common
class TextFieldValue
constructor(
    val annotatedString: AnnotatedString,
    selection: TextRange = TextRange.Zero,
    composition: TextRange? = null,
)

A class holding information about the editing state.

The input service updates text selection, cursor, text and text composition. This class represents those values and it is possible to observe changes to those values in the text editing composables.

This class stores a snapshot of the input state of the edit buffer and provide utility functions for answering IME requests such as getTextBeforeCursor, getSelectedText.

Input service composition is an instance of text produced by IME. An example visual for the composition is that the currently composed word is visually separated from others with underline, or text background. For description of composition please check W3C IME Composition(https://www.w3.org/TR/ime-api/#ime-composition).

IME composition is defined by composition parameter and function. When a TextFieldValue with null composition is passed to a TextField, if there was an active composition on the text, the changes will be applied. Applying a composition will accept the changes that were still being composed by IME. Please use copy functions if you do not want to intentionally apply the ongoing IME composition.

Parameters

annotatedStringthe text to be rendered.
selectionthe selection range. If the selection is collapsed, it represents cursor location. When selection range is out of bounds, it is constrained with the text length.
compositionthe composition range, null means empty composition or apply if a composition exists on the text. Owned by IME, and if you have an instance of TextFieldValue please use copy functions if you do not want to intentionally change the value of this field.

Secondary Constructors

constructor(
    text: String = "",
    selection: TextRange = TextRange.Zero,
    composition: TextRange? = null,
) : this(AnnotatedString(text), selection, composition)

Parameters

textthe text to be rendered.
selectionthe selection range. If the selection is collapsed, it represents cursor location. When selection range is out of bounds, it is constrained with the text length.
compositionthe composition range, null means empty composition or apply if a composition exists on the text. Owned by IME, and if you have an instance of TextFieldValue please use copy functions if you do not want to intentionally change the value of this field.

Properties

Common
val text: String
Common
val selection: TextRange

The selection range. If the selection is collapsed, it represents cursor location. When selection range is out of bounds, it is constrained with the text length.

Common
val composition: TextRange?

Composition range created by IME. If null, there is no composition range.

Input service composition is an instance of text produced by IME. An example visual for the composition is that the currently composed word is visually separated from others with underline, or text background. For description of composition please check W3C IME Composition(https://www.w3.org/TR/ime-api/#ime-composition)

Composition can be set on the by the system, however it is possible to apply an existing composition by setting the value to null. Applying a composition will accept the changes that were still being composed by IME.

Functions

fun copy(
        annotatedString: AnnotatedString = this.annotatedString,
        selection: TextRange = this.selection,
        composition: TextRange? = this.composition,
    ): TextFieldValue

Returns a copy of the TextFieldValue.

fun copy(
        text: String,
        selection: TextRange = this.selection,
        composition: TextRange? = this.composition,
    ): TextFieldValue

Returns a copy of the TextFieldValue.

Companion Object

Properties

Common
val Saver =
        Saver<TextFieldValue, Any>(
            save = {
                arrayListOf(
                    save(it.annotatedString, AnnotatedStringSaver, this),
                    save(it.selection, TextRange.Saver, this),
                )
            },
            restore = {
                @Suppress("UNCHECKED_CAST") val list = it as List<Any>
                TextFieldValue(
                    annotatedString = restore(list[0], AnnotatedStringSaver)!!,
                    selection = restore(list[1], TextRange.Saver)!!,
                )
            },
        )

The default Saver implementation for TextFieldValue.