SpanStyle


Styling configuration that applies at the character level.

Use SpanStyle to configure character-level styles such as text color, font size, font family, background color, and text decorations.

For paragraph-level styling (e.g., alignment, line height), see ParagraphStyle.

import androidx.compose.material3.Text
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.sp

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

val brushColors = listOf(Color.Red, Color.Blue, Color.Green, Color.Yellow)
Text(
    fontSize = 16.sp,
    text =
        buildAnnotatedString {
            withStyle(SpanStyle(brush = Brush.radialGradient(brushColors))) { append("Hello") }
            withStyle(SpanStyle(brush = Brush.radialGradient(brushColors.asReversed()))) {
                append(" World")
            }
        },
)
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString

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()
}

Summary

Public constructors

SpanStyle(
    color: Color,
    fontSize: TextUnit,
    fontWeight: FontWeight?,
    fontStyle: FontStyle?,
    fontSynthesis: FontSynthesis?,
    fontFamily: FontFamily?,
    fontFeatureSettings: String?,
    letterSpacing: TextUnit,
    baselineShift: BaselineShift?,
    textGeometricTransform: TextGeometricTransform?,
    localeList: LocaleList?,
    background: Color,
    textDecoration: TextDecoration?,
    shadow: Shadow?,
    platformStyle: PlatformSpanStyle?,
    drawStyle: DrawStyle?
)

Styling configuration for text spans.

Cmn
SpanStyle(
    brush: Brush?,
    alpha: Float,
    fontSize: TextUnit,
    fontWeight: FontWeight?,
    fontStyle: FontStyle?,
    fontSynthesis: FontSynthesis?,
    fontFamily: FontFamily?,
    fontFeatureSettings: String?,
    letterSpacing: TextUnit,
    baselineShift: BaselineShift?,
    textGeometricTransform: TextGeometricTransform?,
    localeList: LocaleList?,
    background: Color,
    textDecoration: TextDecoration?,
    shadow: Shadow?,
    platformStyle: PlatformSpanStyle?,
    drawStyle: DrawStyle?
)

Styling configuration for text spans.

Cmn

Public functions

SpanStyle
copy(
    color: Color,
    fontSize: TextUnit,
    fontWeight: FontWeight?,
    fontStyle: FontStyle?,
    fontSynthesis: FontSynthesis?,
    fontFamily: FontFamily?,
    fontFeatureSettings: String?,
    letterSpacing: TextUnit,
    baselineShift: BaselineShift?,
    textGeometricTransform: TextGeometricTransform?,
    localeList: LocaleList?,
    background: Color,
    textDecoration: TextDecoration?,
    shadow: Shadow?,
    platformStyle: PlatformSpanStyle?,
    drawStyle: DrawStyle?
)
Cmn
SpanStyle
copy(
    brush: Brush?,
    alpha: Float,
    fontSize: TextUnit,
    fontWeight: FontWeight?,
    fontStyle: FontStyle?,
    fontSynthesis: FontSynthesis?,
    fontFamily: FontFamily?,
    fontFeatureSettings: String?,
    letterSpacing: TextUnit,
    baselineShift: BaselineShift?,
    textGeometricTransform: TextGeometricTransform?,
    localeList: LocaleList?,
    background: Color,
    textDecoration: TextDecoration?,
    shadow: Shadow?,
    platformStyle: PlatformSpanStyle?,
    drawStyle: DrawStyle?
)
Cmn
open operator Boolean
equals(other: Any?)
Cmn
open Int
Cmn
SpanStyle
merge(other: SpanStyle?)

Returns a new span style that is a combination of this style and the given other style.

Cmn
operator SpanStyle
plus(other: SpanStyle)

Plus operator overload that applies a merge.

Cmn
open String
Cmn

Public constructors

SpanStyle

SpanStyle(
    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,
    platformStyle: PlatformSpanStyle? = null,
    drawStyle: DrawStyle? = null
)

Styling configuration for text spans.

Configures character-level styles such as text color, font size, font family, background color, and text decorations.

For paragraph-level styling (e.g., alignment, line height), see ParagraphStyle.

import androidx.compose.material3.Text
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.sp

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.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString

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()
}
import androidx.compose.material3.Text
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.BaselineShift
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.sp

Text(
    fontSize = 20.sp,
    text =
        buildAnnotatedString {
            append(text = "Hello")
            withStyle(SpanStyle(baselineShift = BaselineShift.Superscript, fontSize = 16.sp)) {
                append("superscript")
                withStyle(SpanStyle(baselineShift = BaselineShift.Subscript)) {
                    append("subscript")
                }
            }
        },
)
Parameters
color: Color = Color.Unspecified

color to apply to the text

fontSize: TextUnit = TextUnit.Unspecified

glyph size. If TextUnit.Unspecified, inherits size from parent or default style.

fontWeight: FontWeight? = null

typeface thickness (e.g., bold)

fontStyle: FontStyle? = null

typeface variant (e.g., italic)

fontSynthesis: FontSynthesis? = null

font synthesis rules to fallback to bold/italic if the requested style is missing in fontFamily

fontFamily: FontFamily? = null

font family for rendering

fontFeatureSettings: String? = null

advanced font features in CSS format (e.g., "smcp" for small caps)

letterSpacing: TextUnit = TextUnit.Unspecified

amount of space (in SP or EM) to add between letters. If TextUnit.Unspecified, inherits from parent.

baselineShift: BaselineShift? = null

vertical shift amount from the baseline (e.g., for superscript or subscript)

textGeometricTransform: TextGeometricTransform? = null

geometric transformation to apply

localeList: LocaleList? = null

locale list for region-specific glyphs

background: Color = Color.Unspecified

color of background rectangle covering entire line height from start to end

textDecoration: TextDecoration? = null

decorations (e.g., underline)

shadow: Shadow? = null

shadow effect

platformStyle: PlatformSpanStyle? = null

platform-specific parameters

drawStyle: DrawStyle? = null

drawing style (fill or stroke)

SpanStyle

SpanStyle(
    brush: Brush?,
    alpha: Float = Float.NaN,
    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,
    platformStyle: PlatformSpanStyle? = null,
    drawStyle: DrawStyle? = null
)

Styling configuration for text spans.

Configures character-level styles such as text brush, opacity, font size, font family, background color, and text decorations.

For paragraph-level styling (e.g., alignment, line height), see ParagraphStyle.

import androidx.compose.material3.Text
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.sp

val brushColors = listOf(Color.Red, Color.Blue, Color.Green, Color.Yellow)
Text(
    fontSize = 16.sp,
    text =
        buildAnnotatedString {
            withStyle(SpanStyle(brush = Brush.radialGradient(brushColors))) { append("Hello") }
            withStyle(SpanStyle(brush = Brush.radialGradient(brushColors.asReversed()))) {
                append(" World")
            }
        },
)
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString

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()
}
import androidx.compose.material3.Text
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.BaselineShift
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.sp

Text(
    fontSize = 20.sp,
    text =
        buildAnnotatedString {
            append(text = "Hello")
            withStyle(SpanStyle(baselineShift = BaselineShift.Superscript, fontSize = 16.sp)) {
                append("superscript")
                withStyle(SpanStyle(baselineShift = BaselineShift.Subscript)) {
                    append("subscript")
                }
            }
        },
)
Parameters
brush: Brush?

Brush for painting text, null for unspecified

alpha: Float = Float.NaN

opacity applied to brush (0.0 to 1.0)

fontSize: TextUnit = TextUnit.Unspecified

glyph size. If TextUnit.Unspecified, inherits size from parent or default style.

fontWeight: FontWeight? = null

typeface thickness (e.g., bold)

fontStyle: FontStyle? = null

typeface variant (e.g., italic)

fontSynthesis: FontSynthesis? = null

font synthesis rules to fallback to bold/italic if the requested style is missing in fontFamily

fontFamily: FontFamily? = null

font family for rendering

fontFeatureSettings: String? = null

advanced font features in CSS format (e.g., "smcp" for small caps)

letterSpacing: TextUnit = TextUnit.Unspecified

amount of space (in SP or EM) to add between letters. If TextUnit.Unspecified, inherits from parent.

baselineShift: BaselineShift? = null

vertical shift amount from the baseline (e.g., for superscript or subscript)

textGeometricTransform: TextGeometricTransform? = null

geometric transformation to apply

localeList: LocaleList? = null

locale list for region-specific glyphs

background: Color = Color.Unspecified

color of background rectangle covering entire line height from start to end

textDecoration: TextDecoration? = null

decorations (e.g., underline)

shadow: Shadow? = null

shadow effect

platformStyle: PlatformSpanStyle? = null

platform-specific parameters

drawStyle: DrawStyle? = null

drawing style (fill or stroke)

Public functions

copy

fun 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,
    letterSpacing: TextUnit = this.letterSpacing,
    baselineShift: BaselineShift? = this.baselineShift,
    textGeometricTransform: TextGeometricTransform? = this.textGeometricTransform,
    localeList: LocaleList? = this.localeList,
    background: Color = this.background,
    textDecoration: TextDecoration? = this.textDecoration,
    shadow: Shadow? = this.shadow,
    platformStyle: PlatformSpanStyle? = this.platformStyle,
    drawStyle: DrawStyle? = this.drawStyle
): SpanStyle

copy

fun copy(
    brush: Brush?,
    alpha: Float = this.alpha,
    fontSize: TextUnit = this.fontSize,
    fontWeight: FontWeight? = this.fontWeight,
    fontStyle: FontStyle? = this.fontStyle,
    fontSynthesis: FontSynthesis? = this.fontSynthesis,
    fontFamily: FontFamily? = this.fontFamily,
    fontFeatureSettings: String? = this.fontFeatureSettings,
    letterSpacing: TextUnit = this.letterSpacing,
    baselineShift: BaselineShift? = this.baselineShift,
    textGeometricTransform: TextGeometricTransform? = this.textGeometricTransform,
    localeList: LocaleList? = this.localeList,
    background: Color = this.background,
    textDecoration: TextDecoration? = this.textDecoration,
    shadow: Shadow? = this.shadow,
    platformStyle: PlatformSpanStyle? = this.platformStyle,
    drawStyle: DrawStyle? = this.drawStyle
): SpanStyle

equals

open operator fun equals(other: Any?): Boolean

hashCode

open fun hashCode(): Int

merge

fun merge(other: SpanStyle? = null): SpanStyle

Returns a new span style that is a combination of this style and the given other style.

other span style's null or inherit properties are replaced with the non-null properties of this span style. Another way to think of it is that the "missing" properties of the other style are filled by the properties of this style.

If the given span style is null, returns this span style.

Parameters
other: SpanStyle? = null

style to merge

plus

operator fun plus(other: SpanStyle): SpanStyle

Plus operator overload that applies a merge.

toString

open fun toString(): String

Public properties

alpha

val alphaFloat

Opacity of text. This value is either provided along side Brush, or via alpha channel in color.

background

val backgroundColor

baselineShift

val baselineShiftBaselineShift?

brush

val brushBrush?

Brush to draw text. If not null, overrides color.

color

val colorColor

Color to draw text.

drawStyle

val drawStyleDrawStyle?

fontFamily

val fontFamilyFontFamily?

fontFeatureSettings

val fontFeatureSettingsString?

fontSize

val fontSizeTextUnit

fontStyle

val fontStyleFontStyle?

fontSynthesis

val fontSynthesisFontSynthesis?

fontWeight

val fontWeightFontWeight?

letterSpacing

val letterSpacingTextUnit

localeList

val localeListLocaleList?

platformStyle

val platformStylePlatformSpanStyle?

shadow

val shadowShadow?

textDecoration

val textDecorationTextDecoration?

textGeometricTransform

val textGeometricTransformTextGeometricTransform?