简体   繁体   English

如何在 Jetpack Compose 中勾勒文本

[英]How to outline text in Jetpack Compose

I have a Jetpack Compose Text() element that I'd like to outline in black like so我有一个 Jetpack Compose Text() 元素,我想像这样用黑色勾勒出来这个. .
Anyone know how to do this?有人知道怎么做吗? I've tried using the border() modifier, but that just adds a border around the rectangular area containing the text.我尝试过使用border() 修饰符,但这只是在包含文本的矩形区域周围添加了一个边框。 I've also tried overlaying two text elements, but that doesn't quite work either.我也尝试过覆盖两个文本元素,但这也不太奏效。

You can use a Canvas and the drawIntoCanvas function.您可以使用CanvasdrawIntoCanvas function。

Something like:就像是:

Canvas(
    modifier = Modifier.fillMaxSize(),
    onDraw = {
                drawIntoCanvas {
                    it.nativeCanvas.drawText(
                        "Sample",
                        0f,
                        120.dp.toPx(),
                        textPaintStroke
                    )
                    it.nativeCanvas.drawText(
                        "Sample",
                        0f,
                        120.dp.toPx(),
                        textPaint
                    )
                }
            }
)

with these Paint :用这些Paint

val textPaintStroke = Paint().asFrameworkPaint().apply {
    isAntiAlias = true
    style = android.graphics.Paint.Style.STROKE
    textSize = 64f
    color = android.graphics.Color.BLACK
    strokeWidth = 12f
    strokeMiter= 10f
    strokeJoin = android.graphics.Paint.Join.ROUND
}

val textPaint = Paint().asFrameworkPaint().apply {
    isAntiAlias = true
    style = android.graphics.Paint.Style.FILL
    textSize = 64f
    color = android.graphics.Color.WHITE
}

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM