Jan Veselý
Jan Veselý

Reputation: 1529

Is it possible to center text vertically

Is it possilble as of 1.0.0-alpha03 to align text center vertically without nesting components Stack -> Text

 Text(
       modifier = Modifier
                 .size(150.dp, 30.dp)
                 .background(Colors.ChipGray, RoundedCornerShape(50)),
       textAlign = TextAlign.Center,
       text = start.value.format(DateTimeFormatter.ofPattern("dd. MM. HH:mm")),
      )

Upvotes: 1

Views: 186

Answers (1)

2jan222
2jan222

Reputation: 1934

In General, it is possible but I would not suggest it.

You could use offset to shift the text relative to the background by half of the font size, but I am not sure if you can access the height of the used font here.

        Text(
            text = start.value.format(DateTimeFormatter.ofPattern("dd. MM. HH:mm")),
            textAlign = TextAlign.Center,
            modifier = Modifier
                .size(150.dp, 30.dp)
                .background(Color.Gray, RoundedCornerShape(50))
                .offset(y = fontHeightHalf)
        )

Use the stack instead, like:

        Stack (
            alignment = Alignment.Center,
            modifier = Modifier
                .size(150.dp, 30.dp)
                .background(Color.Gray, RoundedCornerShape(50)),
        ) {
            Text(
                text = start.value.format(DateTimeFormatter.ofPattern("dd. MM. HH:mm")),
                textAlign = TextAlign.Center
            )
        }

Edit: Updated version to 1.0.0-alpha03

Upvotes: 1

Related Questions