Skip to content

Instantly share code, notes, and snippets.

@Oleur
Last active May 13, 2024 17:27
Show Gist options
  • Save Oleur/ca70cd08f51568a0b870333c15ffbca3 to your computer and use it in GitHub Desktop.
Save Oleur/ca70cd08f51568a0b870333c15ffbca3 to your computer and use it in GitHub Desktop.
Draw text on Jetpack Compose Canvas
val textPaint = Paint().asFrameworkPaint().apply {
isAntiAlias = true
textSize = 24.sp.toPx()
color = android.graphics.Color.BLUE
typeface = Typeface.create(Typeface.MONOSPACE, Typeface.BOLD)
}
Canvas(
modifier = modifier.fillMaxSize(),
onDraw = {
drawIntoCanvas {
it.nativeCanvas.drawText(
"My Jetpack Compose Text"
0f, // x-coordinates of the origin (top left)
120.dp.toPx(), // y-coordinates of the origin (top left)
textPaint
)
}
}
)
@devmike01
Copy link

devmike01 commented Oct 3, 2022

There's very little documentation on drawing custom shapes. Thanks for this.

@gybin02
Copy link

gybin02 commented Oct 9, 2023

cannot find this : 24.sp.toPx()

@souravnoobcoder
Copy link

souravnoobcoder commented Apr 17, 2024

@gybin02 add this import
import androidx.compose.ui.unit.sp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment