Created
February 24, 2024 13:47
-
-
Save AndreVero/f9f57b4d2e7d2375ab6410094eabd4cf to your computer and use it in GitHub Desktop.
Text drawing in Jetpack Compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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