Jetpack Compose Text 组件中的等宽数字 [英] Monospace numbers in the Jetpack Compose Text component

查看:54
本文介绍了Jetpack Compose Text 组件中的等宽数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 Jetpack Compose 应用程序中使用自定义字体,并希望在特定文本组件中等宽数字.

对于标准的 Android TextView,这可以使用 fontFeatureSetting="tnum" 属性来完成 (

解决方案

buildAnnotatedString 用于自定义字符串的不同部分.

如果你需要对整个Text应用一些文本样式,你可以使用style参数.

文本(text = String.format(Locale.ROOT, "%02d:%02d", mins, secs),style = LocalTextStyle.current.copy(fontFeatureSettings = "tnum"),)

我在这里使用 LocalTextStyle.current,这是 Text 的默认样式,但您可以将其替换为您需要的样式,例如您的主题之一排版:MaterialTheme.typography.h4

I'm using a custom font within my Jetpack Compose app and would like to monospace the numbers within a specific Text component.

With standard Android TextView, this can be done using the fontFeatureSetting="tnum" attribute (https://stackoverflow.com/a/41143337/2521749).

How can you do this with Jetpack Compose?

解决方案

buildAnnotatedString is needed to customize different parts of a string.

If you need to apply some text style to the whole Text, you can use style argument.

Text(
    text = String.format(Locale.ROOT, "%02d:%02d", mins, secs),
    style = LocalTextStyle.current.copy(fontFeatureSettings = "tnum"),
)

I'm using LocalTextStyle.current here, which is the default style for Text, but you can replace it with the one you need, like one of your theme typography: MaterialTheme.typography.h4

这篇关于Jetpack Compose Text 组件中的等宽数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆