Android Dev/Compose / / 2022. 6. 5. 20:52

Compose - Text outlined color 설정


import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.nativeCanvas
import androidx.compose.ui.unit.dp


@Composable
fun InlineColoredText(
    text : String, setTextSize : Float = 64f ,
    setStrokeWidth : Float = 12f,
    setStrokeMiter : Float = 10f,
    setTextColor : Int = android.graphics.Color.BLACK,
    setBackgroundTextColor : Int = android.graphics.Color.WHITE){

    val textPaintStroke = Paint().asFrameworkPaint().apply {
        isAntiAlias = true
        style = android.graphics.Paint.Style.STROKE
        textSize = setTextSize //64f
        color = setTextColor
        strokeWidth = setStrokeWidth
        strokeMiter = setStrokeMiter
        strokeJoin = android.graphics.Paint.Join.ROUND
    }

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

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

 

 

사용예제

 

 

 

  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유