ritesh
10/07/2021, 8:58 AMHtmlCompat.fromHtml()
in textView.slartus
10/07/2021, 9:14 AMfun charSequenceResource(@StringRes id: Int): CharSequence =
LocalContext.current.resources.getText(id)
fun CharSequence.toAnnotatedString(): AnnotatedString = buildAnnotatedString {
if (this@toAnnotatedString !is Spanned) {
append(this.toString())
return@buildAnnotatedString
}
val spanned = this@toAnnotatedString
append(spanned.toString())
getSpans(0, spanned.length, Any::class.java).forEach { span ->
val start = getSpanStart(span)
val end = getSpanEnd(span)
when (span) {
is StyleSpan -> when (span.style) {
Typeface.BOLD -> addStyle(SpanStyle(fontWeight = FontWeight.Bold), start, end)
Typeface.ITALIC -> addStyle(SpanStyle(fontStyle = FontStyle.Italic), start, end)
Typeface.BOLD_ITALIC -> addStyle(
SpanStyle(
fontWeight = FontWeight.Bold,
fontStyle = FontStyle.Italic
), start, end
)
}
is UnderlineSpan -> addStyle(
SpanStyle(textDecoration = TextDecoration.Underline),
start,
end
)
is ForegroundColorSpan -> addStyle(
SpanStyle(color = Color(span.foregroundColor)),
start,
end
)
}
}
}
title = charSequenceResource(R.string.resource).toAnnotatedString(),
slartus
10/07/2021, 9:15 AMbuildAnnotatedString
ritesh
10/07/2021, 9:45 AMritesh
10/07/2021, 1:40 PMTextView
AndroidView(
modifier = modifier,
factory = { context -> TextView(context) },
update = { it.text = HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_COMPACT) }
)
Source - https://stackoverflow.com/questions/66494838/android-compose-how-to-use-html-tags-in-a-text-viewslartus
10/07/2021, 1:50 PMZach Klippenstein (he/him) [MOD]
10/07/2021, 3:05 PMritesh
10/08/2021, 5:33 AMAnnotatedString
and i am not sure what sub-set of HTML tags or symbols server can give. I believe the best solution in this case is to rely on TextView
Zach Klippenstein (he/him) [MOD]
10/08/2021, 2:05 PMritesh
10/08/2021, 2:17 PMHtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_COMPACT)
and then setting it up in textview in my composable.
AndroidView(
modifier = modifier,
factory = { context -> TextView(context) },
update = { it.text = HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_COMPACT) }
)
This, way i won't have to check for different styles inside when block - Artem's solution.
And i also noticed, it being the recommended solution.
https://developer.android.com/codelabs/jetpack-compose-migration?continue=https%3A%2F%2[…]veloper.android.com%2Fcodelabs%2Fjetpack-compose-migration