🌈 Create new beautiful Compose Gradients with our new wesite ->
Class

TextFieldValue

A class holding information about the editing state.

Source set: Common
public class TextFieldValue public constructor(
    public 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.

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

annotatedString the text to be rendered.
selection 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.
composition the 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

text

Source set: Common
public val text: String

Parameters

text the text to be rendered.
selection 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.
composition the 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.

selection

Source set: Common
public val selection: TextRange = selection.coerceIn(0, text.length)

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.

composition

Source set: Common
public val composition: TextRange? = composition?.coerceIn(0, text.length)

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

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

copy

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

Returns a copy of the TextFieldValue.

copy

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

Returns a copy of the TextFieldValue.

Members

Companion

Source set: Common
public companion object

Properties

Saver

Source set: Common
public val Saver: Saver<TextFieldValue, Any> =
            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.