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.buildAnnotatedString
import androidx.compose.ui.text.withStyle

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
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 a text span.

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 a text span.

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 properties

Float

Opacity of text.

Cmn
Color

The background color for the text.

Cmn
BaselineShift?

The amount by which the text is shifted up from the current baseline.

Cmn
Brush?

Brush to draw text.

Cmn
Color

Color to draw text.

Cmn
DrawStyle?

Drawing style of text, whether fill in the text while drawing or stroke around the edges.

Cmn
FontFamily?

The font family to be used when rendering the text.

Cmn
String?

The advanced typography settings provided by font.

Cmn
TextUnit

The size of glyphs (in logical pixels) to use when painting the text.

Cmn
FontStyle?

The typeface variant to use when drawing the letters (e.g., italic).

Cmn
FontSynthesis?

Whether to synthesize font weight and/or style when the requested weight or style cannot be found in the provided font family.

Cmn
FontWeight?

The typeface thickness to use when painting the text (e.g., bold).

Cmn
TextUnit

The amount of space (in em) to add between each letter.

Cmn
LocaleList?

The locale list used to select region-specific glyphs.

Cmn
PlatformSpanStyle?

Platform specific SpanStyle parameters.

Cmn
Shadow?

The shadow effect applied on the text.

Cmn
TextDecoration?

The decorations to paint on the text (e.g., an underline).

Cmn
TextGeometricTransform?

The geometric transformation applied the text.

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 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.buildAnnotatedString
import androidx.compose.ui.text.withStyle

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
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()
}
Parameters
color: Color = Color.Unspecified

The color to draw the text.

fontSize: TextUnit = TextUnit.Unspecified

The size of glyphs (in logical pixels) to use when painting the text. This may be TextUnit.Unspecified for inheriting from another SpanStyle.

fontWeight: FontWeight? = null

The typeface thickness to use when painting the text (e.g., bold).

fontStyle: FontStyle? = null

The typeface variant to use when drawing the letters (e.g., italic).

fontSynthesis: FontSynthesis? = null

Whether to synthesize font weight and/or style when the requested weight or style cannot be found in the provided font family.

fontFamily: FontFamily? = null

The font family to be used when rendering the text.

fontFeatureSettings: String? = null

The advanced typography settings provided by font. The format is the same as the CSS font-feature-settings attribute: https://www.w3.org/TR/css-fonts-3/#font-feature-settings-prop

letterSpacing: TextUnit = TextUnit.Unspecified

The amount of space (in em) to add between each letter.

baselineShift: BaselineShift? = null

The amount by which the text is shifted up from the current baseline.

textGeometricTransform: TextGeometricTransform? = null

The geometric transformation applied the text.

localeList: LocaleList? = null

The locale list used to select region-specific glyphs.

background: Color = Color.Unspecified

The background color for the text.

textDecoration: TextDecoration? = null

The decorations to paint on the text (e.g., an underline).

shadow: Shadow? = null

The shadow effect applied on the text.

platformStyle: PlatformSpanStyle? = null

Platform specific SpanStyle parameters.

drawStyle: DrawStyle? = null

Drawing style of text, whether fill in the text while drawing or stroke around the edges.

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 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.buildAnnotatedString
import androidx.compose.ui.text.withStyle

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.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()
}
Parameters
brush: Brush?

The brush to use when painting the text. If brush is given as null, it will be treated as unspecified. It is equivalent to calling the alternative color constructor with Color.Unspecified

alpha: Float = Float.NaN

Opacity to be applied to brush from 0.0f to 1.0f representing fully transparent to fully opaque respectively.

fontSize: TextUnit = TextUnit.Unspecified

The size of glyphs (in logical pixels) to use when painting the text. This may be TextUnit.Unspecified for inheriting from another SpanStyle.

fontWeight: FontWeight? = null

The typeface thickness to use when painting the text (e.g., bold).

fontStyle: FontStyle? = null

The typeface variant to use when drawing the letters (e.g., italic).

fontSynthesis: FontSynthesis? = null

Whether to synthesize font weight and/or style when the requested weight or style cannot be found in the provided font family.

fontFamily: FontFamily? = null

The font family to be used when rendering the text.

fontFeatureSettings: String? = null

The advanced typography settings provided by font. The format is the same as the CSS font-feature-settings attribute: https://www.w3.org/TR/css-fonts-3/#font-feature-settings-prop

letterSpacing: TextUnit = TextUnit.Unspecified

The amount of space (in em) to add between each letter.

baselineShift: BaselineShift? = null

The amount by which the text is shifted up from the current baseline.

textGeometricTransform: TextGeometricTransform? = null

The geometric transformation applied the text.

localeList: LocaleList? = null

The locale list used to select region-specific glyphs.

background: Color = Color.Unspecified

The background color for the text.

textDecoration: TextDecoration? = null

The decorations to paint on the text (e.g., an underline).

shadow: Shadow? = null

The shadow effect applied on the text.

platformStyle: PlatformSpanStyle? = null

Platform specific SpanStyle parameters.

drawStyle: DrawStyle? = null

Drawing style of text, whether fill in the text while drawing or stroke around the edges.

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.

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

The background color for the text.

baselineShift

val baselineShiftBaselineShift?

The amount by which the text is shifted up from the current baseline.

brush

val brushBrush?

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

color

val colorColor

Color to draw text.

drawStyle

val drawStyleDrawStyle?

Drawing style of text, whether fill in the text while drawing or stroke around the edges.

fontFamily

val fontFamilyFontFamily?

The font family to be used when rendering the text.

fontFeatureSettings

val fontFeatureSettingsString?

The advanced typography settings provided by font. The format is the same as the CSS font-feature-settings attribute: https://www.w3.org/TR/css-fonts-3/#font-feature-settings-prop

fontSize

val fontSizeTextUnit

The size of glyphs (in logical pixels) to use when painting the text. This may be TextUnit.Unspecified for inheriting from another SpanStyle.

fontStyle

val fontStyleFontStyle?

The typeface variant to use when drawing the letters (e.g., italic).

fontSynthesis

val fontSynthesisFontSynthesis?

Whether to synthesize font weight and/or style when the requested weight or style cannot be found in the provided font family.

fontWeight

val fontWeightFontWeight?

The typeface thickness to use when painting the text (e.g., bold).

letterSpacing

val letterSpacingTextUnit

The amount of space (in em) to add between each letter.

localeList

val localeListLocaleList?

The locale list used to select region-specific glyphs.

platformStyle

val platformStylePlatformSpanStyle?

Platform specific SpanStyle parameters.

shadow

val shadowShadow?

The shadow effect applied on the text.

textDecoration

val textDecorationTextDecoration?

The decorations to paint on the text (e.g., an underline).

textGeometricTransform

val textGeometricTransformTextGeometricTransform?

The geometric transformation applied the text.