{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

SpanStyle

public final class 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.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()
}
See also
AnnotatedString
TextStyle
ParagraphStyle

Summary

Public constructors

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

Styling configuration for a text span.

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

Styling configuration for a text span.

Public methods

final @NonNull SpanStyle
copy(
    @NonNull Color color,
    @NonNull TextUnit fontSize,
    FontWeight fontWeight,
    FontStyle fontStyle,
    FontSynthesis fontSynthesis,
    FontFamily fontFamily,
    String fontFeatureSettings,
    @NonNull TextUnit letterSpacing,
    BaselineShift baselineShift,
    TextGeometricTransform textGeometricTransform,
    LocaleList localeList,
    @NonNull Color background,
    TextDecoration textDecoration,
    Shadow shadow,
    PlatformSpanStyle platformStyle,
    DrawStyle drawStyle
)
final @NonNull SpanStyle
copy(
    Brush brush,
    float alpha,
    @NonNull TextUnit fontSize,
    FontWeight fontWeight,
    FontStyle fontStyle,
    FontSynthesis fontSynthesis,
    FontFamily fontFamily,
    String fontFeatureSettings,
    @NonNull TextUnit letterSpacing,
    BaselineShift baselineShift,
    TextGeometricTransform textGeometricTransform,
    LocaleList localeList,
    @NonNull Color background,
    TextDecoration textDecoration,
    Shadow shadow,
    PlatformSpanStyle platformStyle,
    DrawStyle drawStyle
)
boolean
equals(Object other)
final float

Opacity of text.

final @NonNull Color

The background color for the text.

final BaselineShift

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

final Brush

Brush to draw text.

final @NonNull Color

Color to draw text.

final DrawStyle

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

final FontFamily

The font family to be used when rendering the text.

final String

The advanced typography settings provided by font.

final @NonNull TextUnit

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

final FontStyle

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

final FontSynthesis

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

final FontWeight

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

final @NonNull TextUnit

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

final LocaleList

The locale list used to select region-specific glyphs.

final PlatformSpanStyle

Platform specific SpanStyle parameters.

final Shadow

The shadow effect applied on the text.

final TextDecoration

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

final TextGeometricTransform

The geometric transformation applied the text.

int
final @NonNull SpanStyle

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

final @NonNull SpanStyle

Plus operator overload that applies a merge.

@NonNull String

Public constructors

SpanStyle

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

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.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()
}
Parameters
@NonNull Color color

The color to draw the text.

@NonNull TextUnit fontSize

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

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

FontStyle fontStyle

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

FontSynthesis fontSynthesis

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

FontFamily fontFamily

The font family to be used when rendering the text.

String fontFeatureSettings

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

@NonNull TextUnit letterSpacing

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

BaselineShift baselineShift

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

TextGeometricTransform textGeometricTransform

The geometric transformation applied the text.

LocaleList localeList

The locale list used to select region-specific glyphs.

@NonNull Color background

The background color for the text.

TextDecoration textDecoration

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

Shadow shadow

The shadow effect applied on the text.

PlatformSpanStyle platformStyle

Platform specific SpanStyle parameters.

DrawStyle drawStyle

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

SpanStyle

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

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

float alpha

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

@NonNull TextUnit fontSize

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

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

FontStyle fontStyle

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

FontSynthesis fontSynthesis

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

FontFamily fontFamily

The font family to be used when rendering the text.

String fontFeatureSettings

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

@NonNull TextUnit letterSpacing

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

BaselineShift baselineShift

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

TextGeometricTransform textGeometricTransform

The geometric transformation applied the text.

LocaleList localeList

The locale list used to select region-specific glyphs.

@NonNull Color background

The background color for the text.

TextDecoration textDecoration

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

Shadow shadow

The shadow effect applied on the text.

PlatformSpanStyle platformStyle

Platform specific SpanStyle parameters.

DrawStyle drawStyle

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

Public methods

copy

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

copy

public final @NonNull SpanStyle copy(
    Brush brush,
    float alpha,
    @NonNull TextUnit fontSize,
    FontWeight fontWeight,
    FontStyle fontStyle,
    FontSynthesis fontSynthesis,
    FontFamily fontFamily,
    String fontFeatureSettings,
    @NonNull TextUnit letterSpacing,
    BaselineShift baselineShift,
    TextGeometricTransform textGeometricTransform,
    LocaleList localeList,
    @NonNull Color background,
    TextDecoration textDecoration,
    Shadow shadow,
    PlatformSpanStyle platformStyle,
    DrawStyle drawStyle
)

equals

public boolean equals(Object other)

getAlpha

public final float getAlpha()

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

getBackground

public final @NonNull Color getBackground()

The background color for the text.

getBaselineShift

public final BaselineShift getBaselineShift()

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

getBrush

public final Brush getBrush()

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

getColor

public final @NonNull Color getColor()

Color to draw text.

getDrawStyle

public final DrawStyle getDrawStyle()

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

getFontFamily

public final FontFamily getFontFamily()

The font family to be used when rendering the text.

getFontFeatureSettings

public final String getFontFeatureSettings()

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

getFontSize

public final @NonNull TextUnit getFontSize()

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

getFontStyle

public final FontStyle getFontStyle()

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

getFontSynthesis

public final FontSynthesis getFontSynthesis()

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

getFontWeight

public final FontWeight getFontWeight()

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

getLetterSpacing

public final @NonNull TextUnit getLetterSpacing()

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

getLocaleList

public final LocaleList getLocaleList()

The locale list used to select region-specific glyphs.

getPlatformStyle

public final PlatformSpanStyle getPlatformStyle()

Platform specific SpanStyle parameters.

getShadow

public final Shadow getShadow()

The shadow effect applied on the text.

getTextDecoration

public final TextDecoration getTextDecoration()

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

getTextGeometricTransform

public final TextGeometricTransform getTextGeometricTransform()

The geometric transformation applied the text.

hashCode

public int hashCode()

merge

public final @NonNull SpanStyle merge(SpanStyle other)

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

public final @NonNull SpanStyle plus(@NonNull SpanStyle other)

Plus operator overload that applies a merge.

toString

public @NonNull String toString()