Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Jetpack Compose: TextAlign of SpanStyle

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:
Img

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Expect:
Img

>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
    )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading