Function

withDrawAnnotation

Associates drawAnnotation with the text appended while running block.

DrawAnnotationSample

@Sampled
@Composable
fun DrawAnnotationSample() {
    val cachedPath = remember { Path() }
    val highlight = remember {
        DrawAnnotation(
            drawBehind = {
                textLayoutResult.fillPathForRange(
                    start = start,
                    end = end,
                    target = cachedPath,
                    cornerRadius = CornerRadius(4.dp.toPx()),
                )
                drawPath(cachedPath, color = Color.Yellow.copy(alpha = 0.4f))
            }
        )
    }
    BasicText(
        buildAnnotatedString {
            append("Compose ")
            withDrawAnnotation(highlight) { append("custom draw annotation") }
        }
    )
}
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: