Function

appendInlineContent

Used to insert composables into the text layout.

InlineTextContentSample

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

    val inlineContent =
        mapOf(
            Pair(
                // This tells the [BasicText] 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))
                },
            )
        )

    BasicText(text = text, inlineContent = inlineContent)
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: