Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curved text Jetpack compose

I want to create curved text in Jetpack Compose like it was in "Material You". But how? Example: clock

like image 364
Renattele Renattele Avatar asked Sep 16 '25 21:09

Renattele Renattele


1 Answers

You can do this using Canvas. Compose itself does not have a function to draw a curved text (afaik in rc-01). But using drawIntoCanvas function you can use the nativeCanvas which provides drawTextOnPath where you can draw a text in a Path. In this Path you add an arc, so your text is drawn in this path.

Canvas(
    modifier = Modifier
        .size(300.dp)
        .background(Color.Gray)
) {
    drawIntoCanvas {
        val textPadding = 48.dp.toPx()
        val arcHeight = 400.dp.toPx()
        val arcWidth = 300.dp.toPx()
        val path = Path().apply {
            addArc(0f, textPadding, arcWidth, arcHeight, 180f, 180f)
        }
        it.nativeCanvas.drawTextOnPath(
            "Curved Text with Jetpack Compose",
            path,
            0f,
            0f,
            Paint().apply {
                textSize = 16.sp.toPx()
                textAlign = Paint.Align.CENTER
            }
        )
    }
}

Here's the result:

enter image description here

like image 64
nglauber Avatar answered Sep 18 '25 18:09

nglauber