简体   繁体   English

Jetpack compose 在文本中显示 html

[英]Jetpack compose display html in text

I have a string that contains html, how can I display this in a Jetpack compose Text?我有一个包含 html 的字符串,如何在 Jetpack 撰写文本中显示它?

In a TextView I would use a Spanned and do something like:在 TextView 中,我会使用 Spanned 并执行以下操作:

TextView.setText(Html.fromHtml("<p>something", HtmlCompat.FROM_HTML_MODE_LEGACY)

How can I do this with Text from Jetpack compose?如何使用 Jetpack Compose 中的文本执行此操作?

Same answer as Yhondri, but using HtmlCompat if you are targeting api >24:与 Yhondri 相同的答案,但如果您的目标是 api >24,则使用 HtmlCompat:

@Composable
fun Html(text: String) {
    AndroidView(factory = { context ->
        TextView(context).apply {
            setText(HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY))
        }
    })
}

You can integrate the old TextView into your Jetpack compose like follows:您可以将旧的 TextView 集成到您的 Jetpack 组合中,如下所示:

AndroidView(factory = { context ->
                    TextView(context).apply {
                        text = Html.fromHtml(your_html)
                    }
                })

More info: https://foso.github.io/Jetpack-Compose-Playground/viewinterop/androidview/更多信息: https ://foso.github.io/Jetpack-Compose-Playground/viewinterop/androidview/

Unfortunately, Jetpack compose does NOT support HTML yet...不幸的是,Jetpack compose 还不支持 HTML...

So, what you could do is:所以,你可以做的是:

Option 1: Create your own HTML parser选项 1:创建自己的 HTML 解析器

Jetpack compose supports basic styling such as Bold, color, font etc.. So what you can do is loop through the original HTML text and apply text style manually. Jetpack compose 支持基本样式,例如粗体、颜色、字体等。因此,您可以循环遍历原始 HTML 文本并手动应用文本样式。

Option 2: Integrate the old TextView into your Jetpack compose.选项 2:将旧的TextView集成到您的 Jetpack 组合中。

Please read: Adopting Compose in your app请阅读: 在您的应用中采用 Compose

Thanks.谢谢。

I have done it this way instead of using TextView in AndroidView and it seems to work quite well for me.我已经这样做了,而不是在 AndroidView 中使用 TextView,它似乎对我来说效果很好。 The below composable also wraps up the text and expands when you click on it.当您单击它时,下面的可组合也包含文本并展开。

@Composable
fun ExpandingText(
    description: String,
    modifier: Modifier = Modifier,
    textStyle: TextStyle = MaterialTheme.typography.body2,
    expandable: Boolean = true,
    collapsedMaxLines: Int = 3,
    expandedMaxLines: Int = Int.MAX_VALUE,
) {
    val text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Html.fromHtml(description, Html.FROM_HTML_MODE_LEGACY)
    } else {
        HtmlCompat.fromHtml(description, HtmlCompat.FROM_HTML_MODE_LEGACY)
    }

    var canTextExpand by remember(text) { mutableStateOf(true) }
    var expanded by remember { mutableStateOf(false) }
    val interactionSource = remember { MutableInteractionSource() }

    Text(
        text = text.toString(),
        style = textStyle,
        overflow = TextOverflow.Ellipsis,
        maxLines = if (expanded) expandedMaxLines else collapsedMaxLines,
        modifier = Modifier
            .clickable(
                enabled = expandable && canTextExpand,
                onClick = { expanded = !expanded },
                indication = rememberRipple(bounded = true),
                interactionSource = interactionSource,
            )
            .animateContentSize(animationSpec = spring())
            .then(modifier),
        onTextLayout = {
            if (!expanded) {
                canTextExpand = it.hasVisualOverflow
            }
        }
    )
}

you can use the code below:您可以使用以下代码:

@Composable
private fun TextHtml() {
   Text(text = buildAnnotatedString {
                    withStyle(style = SpanStyle(color = Gray600)) {
                        append("normal text")
                    }
                    withStyle(style = SpanStyle(fontWeight = FontWeight.Bold,color = Gray700)) {
                        append("bold text ")
                    }
                })
}

use withStyle to apply the html tags and use append() inside it to add the string使用 withStyle 应用 html 标签并在其中使用 append() 添加字符串

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM