How to implement Gravity.CENTER_VERTICAL when using SpanStyle?
Do not use other Container Composables like Box, Row, etc…
val spanString = buildAnnotatedString {
withStyle(
SpanStyle(
color = Color(0xFF333333),
fontSize = 32.sp
)
) {
append("Jetpack")
}
withStyle(
SpanStyle(
color = Color(0xFF999999),
fontSize = 14.sp,
fontWeight = FontWeight.Bold
)
) {
append(" Compose")
}
}
Text(
modifier = Modifier.background(color = Color.White),
text = spanString
)
Now:
Expect:

>Solution :
Add this to your second SpanStyle
baselineShift = BaselineShift.Superscript
Modified code
val spanString = buildAnnotatedString {
withStyle(
SpanStyle(
color = Color(0xFF333333),
fontSize = 32.sp
)
) {
append("Jetpack")
}
withStyle(
SpanStyle(
baselineShift = BaselineShift.Superscript, // added line
color = Color(0xFF999999),
fontSize = 14.sp,
fontWeight = FontWeight.Bold
)
) {
append(" Compose")
}
}
Text(
modifier = Modifier.background(color = Color.White),
text = spanString,
textAlign = TextAlign.Center
)