Last active
April 23, 2023 11:38
-
-
Save astamato/663d4ce7fbf62b0bb7271a6181da5cf5 to your computer and use it in GitHub Desktop.
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() | |
.wrapContentHeight(), | |
text = text, | |
onTextLayout = { textLayoutResult -> | |
val newHeight = with(density) { textLayoutResult.size.height.toDp() } | |
if (textLayoutResult.lineCount == 1) { | |
height = newHeight * 2 | |
} | |
}, | |
overflow = TextOverflow.Ellipsis, | |
maxLines = 2, | |
textAlign = TextAlign.Center, | |
// notice the usage of LocalTextStyle.current instead of creating a new TextStyle block | |
style = LocalTextStyle.current.copy( | |
platformStyle = PlatformTextStyle( | |
includeFontPadding = false | |
) | |
) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment