Member-only story

Draw Text With Canvas In Jetpack Compose.

Jayant Kumar🇮🇳
2 min readMay 3, 2024

--

Photo by Neven Krcmarek on Unsplash

In this article we will see how to draw Text with Canvas in Jetpack Compose.

I have already written few articles on canvas.

To draw the text, you need two things ,

  • Canvas Composable function
  • drawText() function
@Composable
fun DrawText() {
val textMeasurer = rememberTextMeasurer()
val text = "Hello"
val textLayoutResult = remember(text) {
textMeasurer.measure(text)
}
Canvas(modifier = Modifier.fillMaxSize()) {
val width = size.width
val height = size.height
drawText(
text = text,
textMeasurer = textMeasurer,
topLeft = Offset(
x = width / 2 - textLayoutResult.size.width,
y = height / 2 - textLayoutResult.size.height
),
style = TextStyle(
fontSize = 30.sp,
fontWeight = FontWeight.Bold
)
)
}
}

As you can see above we called Canvas composable function and provided the full size to the screen.

--

--

Jayant Kumar🇮🇳
Jayant Kumar🇮🇳

Written by Jayant Kumar🇮🇳

Hello My name is Jayant Kumar, I am a software Engineer , specialist in Mobile Development (Android , IOS , Flutter , React Native and Ionic) from India 🇮🇳

No responses yet