Skip to content

Instantly share code, notes, and snippets.

@astamato
Last active April 23, 2023 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astamato/663d4ce7fbf62b0bb7271a6181da5cf5 to your computer and use it in GitHub Desktop.
Save astamato/663d4ce7fbf62b0bb7271a6181da5cf5 to your computer and use it in GitHub Desktop.
@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