applyStyles
fun AnnotatedString.applyStyles(transform: (AnnotatedString.Range<String>) -> SpanStyle?): AnnotatedString
Returns a new AnnotatedString with SpanStyle matching AnnotatedString.Range. The transform function is invoked on each element of the annotations list.
Example
val str = buildAnnotatedString {
pushStringAnnotation("value", "")
append("12")
pop()
pushStringAnnotation("separator", "")
append(" - ")
pop()
pushStringAnnotation("unit", "")
append("hours")
pop()
}.applyStyles { annotation ->
when(annotation.tag) {
"value" -> SpanStyle(color = Color.Red)
"unit" -> SpanStyle(color = Color.Blue)
else -> null // strip 'separator'
}
}
Content copied to clipboard
In the above example, given the AnnotatesString with annotations:
<value>12</value><separator> - </separator><unit>hours</unit>
Content copied to clipboard
the applyStyle()
transform will produce a new AnnotatedString with following styles:
<style color="red">12</style> - <style color="blue">hours</style>
Content copied to clipboard
Parameters
transform
function converting string annotation into SpanStyle. Returning null
value won't apply any style to the annotation range.