Skip to content

Instantly share code, notes, and snippets.

@AndreVero
Created February 24, 2024 13:47
Show Gist options
  • Save AndreVero/f9f57b4d2e7d2375ab6410094eabd4cf to your computer and use it in GitHub Desktop.
Save AndreVero/f9f57b4d2e7d2375ab6410094eabd4cf to your computer and use it in GitHub Desktop.
Text drawing in Jetpack Compose
// Object that will help us to measure and customize text
val textMeasurer = rememberTextMeasurer()
// Calculate all needed info for text drawing
val measurementResult = textMeasurer.measure(
item.title,
constraints = Constraints(
// Set constraints to prevent overflow
maxWidth = (innerCircleRadius.toPx() * 2 - 16.dp.toPx()).toInt()
),
// Set style for the text
style = textStyle.copy(
textAlign = TextAlign.Center,
fontSize = textStyle.fontSize
)
)
drawText(
textLayoutResult = measurementResult,
// For text we need to set top left corner from which drawing will be started
// in this case I set text in the middle
topLeft = Offset(
x = currentOffset.x - measurementResult.size.width / 2,
y = currentOffset.y + pathBounds.height
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment