Styling configuration for a text span.
AnnotatedStringBuilderSample
fun AnnotatedStringBuilderSample() {
buildAnnotatedString {
append("Hello")
// push green text style so that any appended text will be green
pushStyle(SpanStyle(color = Color.Green))
// append new text, this text will be rendered as green
append(" World")
// pop the green style
pop()
// append a string without style
append("!")
// then style the last added word as red, exclamation mark will be red
addStyle(SpanStyle(color = Color.Red), "Hello World".length, this.length)
toAnnotatedString()
}
}
SpanStyleSample
@Composable
fun SpanStyleSample() {
Text(
fontSize = 16.sp,
text =
buildAnnotatedString {
withStyle(style = SpanStyle(color = Color.Red)) { append("Hello") }
withStyle(SpanStyle(color = Color.Blue)) { append(" World") }
},
)
}