在Android Jetpack Compose UI中,我们如何将文本格式化为上标或下标?

huangapple go评论152阅读模式
英文:

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

问题

我正在使用Android Jetpack Compose示例中,我正在显示一些文本方程式,如下所示:

<body>

<!-- 上标 -->
<p> E = mc<sup>2</sup></p>

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

是否存在任何文本装饰或样式机制,我可以使用它来实现相同的输出?

英文:

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

&lt;html&gt;
&lt;body&gt;

&lt;!-- Superscript--&gt;
&lt;p&gt; E = mc&lt;sup&gt;2&lt;/sup&gt;&lt;/p&gt;

&lt;!--Subscript--&gt;
&lt;p&gt; CH&lt;sub&gt;4&lt;/sub&gt; + H&lt;sub&gt;2&lt;/sub&gt;O = CO + 3H&lt;sub&gt;2&lt;/sub&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;  

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

答案1

得分: 20

更新: 请参考此处提到的新答案:https://stackoverflow.com/a/66801935/2949612

使用 BaselineShift,我们可以为 Text 小部件使用 Span,这允许装饰文本作为上标或下标。

以下是实现上述输出的工作代码:

@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 = &quot;E = mc&quot;, style = defaultStyle) {
            Span(
                    text = &quot;2&quot;,
                    style =scriptStyleSuper
            ) {
                Span(text = &quot;\n&quot;)
                Span(text = &quot;CH&quot;, style = defaultStyle)
                Span(text = &quot;4 &quot;,style = scriptStyleSub)
                Span(text = &quot;+ H&quot;, style = defaultStyle)
                Span(text = &quot;2&quot;,style = scriptStyleSub)
                Span(text = &quot;O = CO + 3H&quot;, style = defaultStyle)
                Span(text = &quot;2&quot;,style = scriptStyleSub)
            }
        }
    }
}

输出:

在Android Jetpack Compose UI中,我们如何将文本格式化为上标或下标?

要查看更多信息,请访问:https://developer.android.com/reference/kotlin/androidx/compose/ui/text/style/package-summary

英文:

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 = &quot;E = mc&quot;, style = defaultStyle) {
            Span(
                    text = &quot;2&quot;,
                    style =scriptStyleSuper
            ) {
                Span(text = &quot;\n&quot;)
                Span(text = &quot;CH&quot;, style = defaultStyle)
                Span(text = &quot;4 &quot;,style = scriptStyleSub)
                Span(text = &quot;+ H&quot;, style = defaultStyle)
                Span(text = &quot;2&quot;,style = scriptStyleSub)
                Span(text = &quot;O = CO + 3H&quot;, style = defaultStyle)
                Span(text = &quot;2&quot;,style = scriptStyleSub)
            }
        }
      }
    }
    

Output:

在Android Jetpack Compose UI中,我们如何将文本格式化为上标或下标?

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

答案2

得分: 12

你可以使用BaselineShift.SuperscriptBaselineShift.Subscript来定义一个SpanStyle

类似这样:

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

然后,你可以使用AnnotatedString来显示具有多种样式的文本。

类似这样:

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

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")
        }
    }
)

在Android Jetpack Compose UI中,我们如何将文本格式化为上标或下标?

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")
        }
    }
)

在Android Jetpack Compose UI中,我们如何将文本格式化为上标或下标?

英文:

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(&quot;E = mc&quot;)
        withStyle( superscript) {
            append(&quot;2&quot;)
        }
    }
)

在Android Jetpack Compose UI中,我们如何将文本格式化为上标或下标?

and

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

在Android Jetpack Compose UI中,我们如何将文本格式化为上标或下标?

答案3

得分: 0

请直接使用Unicode,每个符号都可以找到一个Unicode。

英文:

Please use unicode directly, for every symbol you will be able to find one unicode.

huangapple
  • 本文由 发表于 2020年1月6日 02:17:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/59602862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定