弯曲的文本 Jetpack 撰写 [英] Curved text Jetpack compose

查看:32
本文介绍了弯曲的文本 Jetpack 撰写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Jetpack Compose 中创建弯曲的文本,就像在Material You"中一样.但是如何?例子:

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

推荐答案

您可以使用 Canvas 执行此操作.Compose 本身没有绘制曲线文本的功能(afaik in rc-01).但是使用 drawIntoCanvas 函数,您可以使用 nativeCanvas ,它提供了 drawTextOnPath,您可以在其中绘制 Path 中的文本.在此 Path 中,您添加了一条弧线,因此您的文本将在此路径中绘制.

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
            }
        )
    }
}

结果如下:

这篇关于弯曲的文本 Jetpack 撰写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆