Android 12 Developer Preview is here! Try it out, and give us your feedback!

SpanStyle

@Immutable class SpanStyle
kotlin.Any
   ↳ androidx.compose.ui.text.SpanStyle

Styling configuration for a text span. This configuration only allows character level styling, in order to set paragraph level styling such as line height, or text alignment please see ParagraphStyle.

import androidx.compose.material.Text
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.text.buildAnnotatedString

Text(
    fontSize = 16.sp,
    text = buildAnnotatedString {
        withStyle(style = SpanStyle(color = Color.Red)) {
            append("Hello")
        }
        withStyle(SpanStyle(color = Color.Blue)) {
            append(" World")
        }
    }
)
import androidx.compose.ui.text.SpanStyle

with(AnnotatedString.Builder("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()
}

Summary

Public constructors
<init>(color: Color = Color.Unspecified, fontSize: TextUnit = TextUnit.Unspecified, fontWeight: FontWeight? = null, fontStyle: FontStyle? = null, fontSynthesis: FontSynthesis? = null, fontFamily: FontFamily? = null, fontFeatureSettings: String? = null, letterSpacing: TextUnit = TextUnit.Unspecified, baselineShift: BaselineShift? = null, textGeometricTransform: TextGeometricTransform? = null, localeList: LocaleList? = null, background: Color = Color.Unspecified, textDecoration: TextDecoration? = null, shadow: Shadow? = null)

Styling configuration for a text span.

Public methods
SpanStyle
copy(color: Color = this.color, fontSize: TextUnit = this.fontSize, fontWeight: FontWeight? = this.fontWeight, fontStyle: FontStyle? = this.fontStyle, fontSynthesis: FontSynthesis? = this.fontSynthesis, fontFamily: FontFamily? = this.fontFamily, fontFeatureSettings: String? = this.fontFeatureSettings,