【问题标题】:How to outline text in Jetpack Compose如何在 Jetpack Compose 中勾勒文本
【发布时间】:2021-04-05 18:55:08
【问题描述】:

我有一个 Jetpack Compose Text() 元素,我想像 这样用黑色勾勒出轮廓。
有人知道怎么做吗? 我尝试使用border() 修饰符,但这只是在包含文本的矩形区域周围添加了一个边框。我也尝试过覆盖两个文本元素,但这也不太奏效。

【问题讨论】:

    标签: android android-jetpack-compose android-jetpack-compose-text


    【解决方案1】:

    您可以使用CanvasdrawIntoCanvas 函数。

    类似:

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

    这些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
    }
    

    【讨论】:

      猜你喜欢
      • 2012-07-04
      • 1970-01-01
      • 2021-12-04
      • 2020-12-22
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      • 2023-01-18
      相关资源
      最近更新 更多