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

InlineTextContent

@Immutable data class InlineTextContent
kotlin.Any
   ↳ androidx.compose.foundation.text.InlineTextContent

A data class that stores a composable to be inserted into the text layout.

Different from a regular composable, a Placeholder is also needed for text layout to reserve space. In this placeholder, the size of the content and how it will be aligned within the text line is defined. When the children composable is measured, its size given in Placeholder.width and Placeholder.height will be converted into androidx.compose.ui.unit.Constraints and passed through androidx.compose.ui.Layout.

import androidx.compose.foundation.Box
import androidx.compose.foundation.Text
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.foundation.text.appendInlineContent
import androidx.compose.ui.text.Placeholder
import androidx.compose.ui.text.annotatedString

val myId = "inlineContent"
val text = annotatedString {
    append("Hello")
    // Append a placeholder string "[myBox]" and attach an annotation "inlineContent" on it.
    appendInlineContent(myId, "[myBox]")
}

val inlineContent = mapOf(
    Pair(
        // This tells the [CoreText] to replace the placeholder string "[myBox]" by
        // the composable given in the [InlineTextContent] object.
        myId,
        InlineTextContent(
            // Placeholder tells text layout the expected size and vertical alignment of
            // children composable.
            Placeholder(
                width = 0.5.em,
                height = 0.5.em,
                placeholderVerticalAlign = PlaceholderVerticalAlign.AboveBaseline
            )
        ) {
            // This [Box] will fill maximum size, which is specified by the [Placeholder]
            // above. Notice the width and height in [Placeholder] are specified in TextUnit,
            // and are converted into pixel by text layout.
            Box(modifier = Modifier.fillMaxSize().background(color = Color.Red))
        }
    )
)

Text(text = text, inlineContent = inlineContent)

Summary

Public constructors
<init>(placeholder: Placeholder, children: (String) -> Unit)

A data class that stores a composable to be inserted into the text layout.

Properties
(String) -> Unit

The composable to be inserted into the text layout.

Placeholder

The setting object that defines the size and vertical alignment of this composable in the text line.

Public constructors

<init>

InlineTextContent(
    placeholder: Placeholder,
    children: (String) -> Unit)

A data class that stores a composable to be inserted into the text layout.

Different from a regular composable, a Placeholder is also needed for text layout to reserve space. In this placeholder, the size of the content and how it will be aligned within the text line is defined. When the children composable is measured, its size given in Placeholder.width and Placeholder.height will be converted into androidx.compose.ui.unit.Constraints and passed through androidx.compose.ui.Layout.

import androidx.compose.foundation.Box
import androidx.compose.foundation.Text
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.foundation.text.appendInlineContent
import androidx.compose.ui.text.Placeholder
import androidx.compose.ui.text.annotatedString

val myId = "inlineContent"
val text = annotatedString {
    append("Hello")
    // Append a placeholder string "[myBox]" and attach an annotation "inlineContent" on it.
    appendInlineContent(myId, "[myBox]")
}

val inlineContent = mapOf(
    Pair(
        // This tells the [CoreText] to replace the placeholder string "[myBox]" by
        // the composable given in the [InlineTextContent] object.
        myId,
        InlineTextContent(
            // Placeholder tells text layout the expected size and vertical alignment of
            // children composable.
            Placeholder(
                width = 0.5.em,
                height = 0.5.em,
                placeholderVerticalAlign = PlaceholderVerticalAlign.AboveBaseline
            )
        ) {
            // This [Box] will fill maximum size, which is specified by the [Placeholder]
            // above. Notice the width and height in [Placeholder] are specified in TextUnit,
            // and are converted into pixel by text layout.
            Box(modifier = Modifier.fillMaxSize().background(color = Color.Red))
        }
    )
)

Text(text = text, inlineContent = inlineContent)

Properties

children

val children: (String) -> Unit

The composable to be inserted into the text layout. The string parameter passed to it will the alternateText given to appendInlineContent.

placeholder

val placeholder: Placeholder

The setting object that defines the size and vertical alignment of this composable in the text line. This is different from the measure of Layout

See Also