How to show vertical text with proper size/layout in jetpack compose
My version. After a few tests it seems to work pretty well
class ComposeActivity7 : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ComposeTutorialTheme {
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
modifier = Modifier
.padding(4.dp)
.background(Color.Gray),
text = "This is at the top"
)
Row(
modifier = Modifier
.fillMaxWidth()
.weight(1f),
verticalAlignment = Alignment.CenterVertically
) {
Text(
modifier = Modifier
.vertical()
.rotate(-90f)
.background(Color.Gray)
.padding(4.dp),
text = "This should be vertical text on the left side"
)
Text(
modifier = Modifier
.fillMaxSize()
.background(Color.Yellow),
textAlign = TextAlign.Center,
text = "This is a big yellow box that should take up most of the space"
)
}
}
}
}
}
}
fun Modifier.vertical() =
layout { measurable, constraints ->
val placeable = measurable.measure(constraints)
layout(placeable.height, placeable.width) {
placeable.place(
x = -(placeable.width / 2 - placeable.height / 2),
y = -(placeable.height / 2 - placeable.width / 2)
)
}
}
Result