简体   繁体   English

如何在 jetpack compose 中突出显示文本的特定单词?

[英]How to highlight specific word of the text in jetpack compose?

I wanted to know how to highlight the specific part of the text in jetpack compose.我想知道如何在 Jetpack Compose 中突出显示文本的特定部分。 I tried Html.fromHtml() like this我这样试过Html.fromHtml()

Text(text = Html.fromHtml(" <font color='red'> Hello </font> World").toString())

But it didn't work.但它没有用。 Is there any way I can do this in compose?有什么办法可以在撰写中做到这一点?

With 1.0.0-beta06 you can use the AnnotatedString to display the text with multiple styles.使用1.0.0-beta06 ,您可以使用AnnotatedString显示具有多个 styles 的文本。

Something like:就像是:

Text(buildAnnotatedString {
    withStyle(style = SpanStyle(color = Color.Red)) {
        append("Hello")
    }
    append(" World ")
})

在此处输入图像描述

Check this function below.检查下面的 function。 Here paragraph is your string source and searchQuery is the specific text you want to highlight.这里的段落是您的字符串源,而 searchQuery 是您要突出显示的特定文本。

This provides you a dynamic state for text and search highlights.这为您提供了动态 state 用于文本和搜索亮点。

@Composable
    fun getData(): StateFlow<AnnotatedString?> {

        val span = SpanStyle(
            color = MaterialTheme.colorScheme.onPrimaryContainer,
            fontWeight = FontWeight.SemiBold,
            background = MaterialTheme.colorScheme.primaryContainer
        )

        return combine(paragraph, searchQuery) { text, query ->
            buildAnnotatedString {
                var start = 0
                while (text.indexOf(query, start, ignoreCase = true) != -1 && query.isNotBlank()) {
                    val firstIndex = text.indexOf(query, start, true)
                    val end = firstIndex + query.length
                    append(text.substring(start, firstIndex))
                    withStyle(style = span) {
                        append(text.substring(firstIndex, end))
                    }
                    start = end
                }
                append(text.substring(start, text.length))
                toAnnotatedString()
            }
        }.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), null)
    }

You can use AnnotatedString to append each word/section with it's own style or to add different style at any index which is great if you're using a string resource.您可以将AnnotatedString用于 append 每个单词/部分具有自己的样式,或者在任何索引处添加不同的样式,如果您使用的是字符串资源,这非常有用。

For the hello world example you could construct something like this:对于 hello world 示例,您可以构建如下内容:


val annotatedString = buildAnnotatedString {
    val str = "Hello World" // or stringResource(id = R.string.hello_world)
    val boldStr = "Hello" // or stringResource(id = R.string.hello)
    val startIndex = str.indexOf(boldStr)
    val endIndex = startIndex + boldStr.length
    append(str)
    addStyle(style = SpanStyle(color = Color.Red), start = startIndex, end = endIndex)
}
Text(
    text = annotatedString,
)

你好世界文本与红色的你好字母

Using addStyle in this way allows us to do some fun things like adding multiple styles to the same text以这种方式使用addStyle可以让我们做一些有趣的事情,比如将多个 styles 添加到同一文本

val annotatedString = buildAnnotatedString {
    val str = "Hello Wonderful World" // or stringResource(id = R.string.hello_world)
    val boldStr = "Wonderful World" // or stringResource(id = R.string.world)
    val startIndex = str.indexOf(boldStr)
    val endIndex = startIndex + boldStr.length
    append(str)
    addStyle(style = SpanStyle(color = Color.Red), start = startIndex, end = endIndex)

    val italicsStr = "Wonderful"
    val italicsStartIndex = str.indexOf(italicsStr)
    val italicsEndIndex = startIndex + italicsStr.length
    addStyle(style = SpanStyle(fontStyle = FontStyle.Italic), start = italicsStartIndex, end = italicsEndIndex)
}
Text(
    text = annotatedString,
    style = TextStyle(fontWeight = FontWeight.Bold),
    color = Color.Blue,
)

文本你好美妙的世界,颜色和斜体各不相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Jetpack Compose 中 Text 的最后一个单词处添加图标 - Add icon at last word of Text in Jetpack Compose 如何在 android 喷气背包撰写测试中断言文本不包含特定字符? - how to assert that text not contains specific characters in android jetpack compose testing? 如何在 Jetpack Compose 中勾勒文本 - How to outline text in Jetpack Compose Android Jetpack Compose:如何使文本利用完整的行空间并在溢出时将单词换行? - Android Jetpack Compose: How to make Text utilize complete row space and break the word to new line in case of overflow? 如何使用 Jetpack Compose 将 BottomSheetScaffold 扩展到特定高度? - How to expand BottomSheetScaffold to a specific height at with Jetpack Compose? Jetpack Compose 如何将具有特定大小的文本或文本组件的内容与左下角或右下角对齐? - Jetpack Compose How to Align text or content of Text component with specific size to bottom left or right? 如何使用 Jetpack Compose 在文本中制作中间省略号 - How to make middle ellipsis in Text with Jetpack Compose 如何居中对齐按钮文本(Jetpack Compose) - How to center align a button text (Jetpack Compose) 如何在 android Jetpack Compose 中删除文本? - How to strikethrough Text in android Jetpack Compose? 如何在 Jetpack Compose 中使文本可滚动 - How to make Text scrollable in Jetpack Compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM