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
package com.example.showsnackbarsample | |
import android.os.Bundle | |
import android.util.Log | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.ExperimentalLayoutApi | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.foundation.layout.fillMaxWidth |
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
@Composable | |
fun TwoLinesCenterText( | |
text: String | |
) { | |
var height by remember { mutableStateOf(Dp.Unspecified) } | |
val density = LocalDensity.current | |
Text( | |
modifier = Modifier | |
.height(height) | |
.fillMaxWidth() |
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
@Composable | |
fun TwoLinesCenterText(mainText: @Composable () -> Unit, modifier: Modifier = Modifier) { | |
SubcomposeLayout(modifier) { constraints -> | |
val twoLineMeasureable: List<Placeable> = | |
subcompose(SlotsEnum.Main) { Text("\n") }.map { | |
it.measure(constraints) | |
} | |
val size = twoLineMeasureable.fold(IntSize.Zero) { currentMax, placeable -> | |
IntSize( |
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
fun TwoLinesCenterText( | |
text: String, modifier: Modifier = Modifier | |
) { | |
val textMeasurer = rememberTextMeasurer() | |
val textPlaceholderMeasurement = remember { textMeasurer.measure(text = "\n" ) } | |
val newHeight = with(LocalDensity.current) { textPlaceholderMeasurement.size.height.toDp() } | |
Box( | |
modifier = modifier |
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
@Composable | |
fun TwoLinesCenterText( | |
text: String | |
) { | |
val textMeasurer = rememberTextMeasurer() | |
Text( | |
text = text, | |
modifier = modifier | |
.fillMaxWidth() | |
.layout { measurable, constraints -> |
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
@Composable | |
fun TwoLinesCenterText( | |
text: String | |
) { | |
var height by remember { mutableStateOf(0.dp) } | |
val density = LocalDensity.current | |
if (height == 0.dp) { | |
Text( | |
text = "\n", |
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
@Composable | |
fun TwoLinesCenterText( | |
text: String | |
) { | |
var height by remember { mutableStateOf(Dp.Unspecified) } | |
val density = LocalDensity.current | |
Text( | |
modifier = Modifier | |
.height(height) |
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
@Composable | |
fun TwoLinesCenterText( | |
text: String, modifier: Modifier = Modifier | |
) { | |
val textStyle = LocalTextStyle.current | |
val textMeasurer = rememberTextMeasurer() | |
val twoLinesHeightMeasurement = remember(textStyle, textMeasurer) { | |
textMeasurer.measure( | |
text = "\n", |
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
Box(contentAlignment = Alignment.Center) { | |
// Empty text for vertical alignment purposes. | |
Text( | |
"\n", | |
textAlign = TextAlign.Center, | |
) | |
Text( | |
"Hola mundo!", | |
overflow = Ellipsis, | |
maxLines = 2, |
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
// Calculate the height of a text occupying 2 lines. | |
val textHeight = calculateTextHeight(text = LONG_TEXT, maxLines = 2) | |
Text( | |
// Set the actual text parameter to Text composable. | |
text = MY_TEXT, | |
modifier = Modifier | |
// Configure the calculated height | |
.height(textHeight) | |
// Align vertically inside the available measure region |
NewerOlder