Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How we can we format Text as superscript or subscript in Android Jetpack Compose UI?

I am working on one example using Android Jetpack Compose, where I am displaying some text equations like below :

<html>
<body>

<!-- Superscript-->
<p> E = mc<sup>2</sup></p>

<!--Subscript-->
<p> CH<sub>4</sub> + H<sub>2</sub>O = CO + 3H<sub>2</sub></p>
</body>
</html>  

Does there any decoration or style mechanism exist for Text that I can use to achieve the same output?

like image 896
pRaNaY Avatar asked Jan 05 '20 18:01

pRaNaY


People also ask

How do you change the font on jetpack compose?

To change font size of Text composable in Android Jetpack Compose, pass a required font size value for the optional fontSize parameter of Text composable. Make sure to import sp , as shown in the above code snippet, when you are assign fontSize property with scale-independent pixels.

How do I make Text bold in jetpack?

To change font weight of Text composable to Bold, in Android Jetpack Compose, pass FontWeight. Bold for the optional fontWeight parameter of Text composable.

How do I change the Text color in compose?

To change color of Text composable in Android Jetpack Compose, pass a required Color value for the optional color parameter of Text composable.

What is kotlin jetpack compose?

Jetpack Compose is Android's modern toolkit for building native UI. It simplifies and accelerates UI development on Android. Quickly bring your app to life with less code, powerful tools, and intuitive Kotlin APIs.


2 Answers

UPDATE: Please refer new answer mentioned here: https://stackoverflow.com/a/66801935/2949612


Using BaselineShift, we can use Span for Text widget, which allows to decorate text as subscript or superscript.

Below is the working code to achieve the above output:

    @Composable
    fun Equations(name: String) {
    val defaultStyle = TextStyle(fontSize = 20.sp,
            color = Color.White)
    val scriptStyleSuper = TextStyle(
            baselineShift = BaselineShift.Superscript,
            fontSize = 12.sp,
            color = Color.Green)
    val scriptStyleSub = TextStyle(
            baselineShift = BaselineShift.Subscript,
            fontSize = 12.sp,
            color = Color.Green)
    Text {
        Span(text = "E = mc", style = defaultStyle) {
            Span(
                    text = "2",
                    style =scriptStyleSuper
            ) {
                Span(text = "\n")
                Span(text = "CH", style = defaultStyle)
                Span(text = "4 ",style = scriptStyleSub)
                Span(text = "+ H", style = defaultStyle)
                Span(text = "2",style = scriptStyleSub)
                Span(text = "O = CO + 3H", style = defaultStyle)
                Span(text = "2",style = scriptStyleSub)
            }
        }
      }
    }
    

Output:

jetpack compose baseline shift

To check more info: https://developer.android.com/reference/kotlin/androidx/compose/ui/text/style/package-summary

like image 181
pRaNaY Avatar answered Oct 13 '22 04:10

pRaNaY


With 1.0.0-beta03 you can define a SpanStyle using the BaselineShift.Superscript and BaselineShift.Subscript.

Something like:

    val superscript = SpanStyle(
        baselineShift = BaselineShift.Superscript,
        fontSize = 16.sp,
        color = Color.Red
    )
    val subscript = SpanStyle(
        baselineShift = BaselineShift.Subscript,
        fontSize = 16.sp,
        color = Color.Blue
    )

Then you can use the AnnotatedString to display the text with multiple styles.

Something like:

Text(
    fontSize = 20.sp,
    text = buildAnnotatedString {
        append("E = mc")
        withStyle( superscript) {
            append("2")
        }
    }
)

enter image description here

and

    Text(
        fontSize = 20.sp,
        text = buildAnnotatedString {
            append("CH")
            withStyle( subscript) {
                append("4")
            }
            append(" + H")
            withStyle( subscript) {
                append("2")
            }
            append("O = CO + 3H")
            withStyle( subscript) {
                append("2")
            }
        }
    )

enter image description here

like image 23
Gabriele Mariotti Avatar answered Oct 13 '22 04:10

Gabriele Mariotti