Nicolai
01/31/2023, 9:19 AMFilip Wiesner
01/31/2023, 9:27 AM@Composable
fun calculateTextWidth(
text: String,
style: TextStyle,
maxIntrinsicSize: Boolean = true
): Dp {
val density = LocalDensity.current
val fontResolver = LocalFontFamilyResolver.current
return remember(text, style, maxIntrinsicSize, density, fontResolver) {
ParagraphIntrinsics(
text = text,
style = style,
fontFamilyResolver = fontResolver,
density = density
).let { px ->
with(density) {
if (maxIntrinsicSize) px.maxIntrinsicWidth.toDp()
else px.minIntrinsicWidth.toDp()
}
}
}
}
It was almost a year ago so I am not sure if this is still valid but the point is that you can get width of some (one line) text without the need of actually drawing it to screen.Nicolai
01/31/2023, 9:34 AM