Nathan Castlehow
08/17/2021, 3:16 AMColton Idle
08/17/2021, 3:23 AMNathan Castlehow
08/17/2021, 3:23 AMColton Idle
08/17/2021, 3:39 AMHalil Ozercan
08/17/2021, 5:55 AMTextLayoutResult
but it makes you lose a frame. I wanted to check whether it's possible with SubcomposeLayout
. I'm not able to test it out right now but the idea is basically;
SubcomposeLayout { constraints ->
val measurable = subcompose("3chars") {
Text("WWW", ...)
}
// get the supposed width from this subcomposition but don't place it in the final layout
val actualPlaceable = subcompose("") {
Text("..,", )
}.single().measure(constraints.constrain(...))
layout(a, b) {
actualPlaceable.placeRelative...
}
}
Nathan Castlehow
08/18/2021, 3:54 AM@Composable
fun TextWithReservedSpace(
text: String,
modifier: Modifier = Modifier,
color: Color = Color.Unspecified,
style: TextStyle = LocalTextStyle.current,
reservedCharLength: Int,
) {
val reservedWidthString = remember(reservedCharLength) {
"W".repeat(reservedCharLength)
}
SubcomposeLayout { constraints ->
// get the supposed width from this subcomposition but don't place it in the final layout
val measurable = subcompose("reserved_space") {
Text(
reservedWidthString,
style = style,
modifier = modifier,
)
}.single().measure(constraints)
val actualPlaceable = subcompose("") {
Text(
text,
color = color,
style = style,
modifier = modifier
)
}.single().measure(constraints)
layout(measurable.width, measurable.height) {
actualPlaceable.placeRelative(measurable.width - actualPlaceable.width, 0)
}
}
}
Halil Ozercan
08/18/2021, 6:30 AMNathan Castlehow
08/18/2021, 7:08 AM