-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Fix] annotatedStringResource
with args don't render annotation styles
#253
Comments
thomas-leguellec
added
the
✍️ To refine
The ticket must to be refine during "Refinement meeting"
label
Mar 23, 2023
thomas-leguellec
added
✅ Ready
All the needed informations are detailed within the ticket to include it in another sprint
and removed
✍️ To refine
The ticket must to be refine during "Refinement meeting"
labels
Apr 5, 2023
thomas-leguellec
changed the title
[Fix] Aug 29, 2023
annotatedStringResource
with args don't render annotation stylesannotatedStringResource
with args don't render annotation styles
@soulcramer, happy to report that this solution works pretty well: <string name="hello">Hello, <b><annotation variable="who" typography="large" color="main">who</annotation></b>!</string> @Composable
public fun annotatedStringResource(@StringRes id: Int, args: PersistentMap<String, String>): AnnotatedString {
val resources = resources()
val density = LocalDensity.current
val colors = SparkTheme.colors
val typography = SparkTheme.typography
return remember(id, values) {
resources
.buildSpannedStringWithArgs(id, args)
.asAnnotatedString(density, colors, typography)
}
}
private fun Resources.buildSpannedStringWithArgs(
@StringRes id: Int,
args: PersistentMap<String, String>,
): SpannedString = buildSpannedString {
append(getText(id))
getSpans(0, length, Annotation::class.java)
.filterIsInstance<Annotation>()
.filter { it.key == "variable" }
.forEach { replace(getSpanStart(it), getSpanEnd(it), args.getValue(it.value)) }
} @Preview
@Composable
private fun AnnotatedStringResourcePreview() {
PreviewTheme {
Text(
text = annotatedStringResource(
R.string.spark_annotatedStringResource_test,
persistentMapOf("who" to "Bob"),
),
)
}
} |
Nice! I'll add it then 🥳 |
thomas-leguellec
removed
the
✅ Ready
All the needed informations are detailed within the ticket to include it in another sprint
label
Jan 10, 2024
3 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Currently we're converting the spanned string from
getText
into html to be able to insert the args in their format placeholder and then convert this html back to a spanned string. Sadly this doesn't support theannotation
tag as it's not part of the html spec.I found this lib that might help us resolve this 🤔
The text was updated successfully, but these errors were encountered: