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

TextStyle

public final class TextStyle


Styling configuration for a Text.

import androidx.compose.material.Text
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.em
import androidx.compose.ui.unit.sp

Text(
    text = "Demo Text",
    style = TextStyle(
        color = Color.Red,
        fontSize = 16.sp,
        fontFamily = FontFamily.Monospace,
        fontWeight = FontWeight.W800,
        fontStyle = FontStyle.Italic,
        letterSpacing = 0.5.em,
        background = Color.LightGray,
        textDecoration = TextDecoration.Underline
    )
)
See also
AnnotatedString
SpanStyle
ParagraphStyle

Summary

Nested types

public static class TextStyle.Companion

Public constructors

TextStyle(
    @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,
    DrawStyle drawStyle,
    TextAlign textAlign,
    TextDirection textDirection,
    @NonNull TextUnit lineHeight,
    TextIndent textIndent,
    PlatformTextStyle platformStyle,
    LineHeightStyle lineHeightStyle,
    LineBreak lineBreak,
    Hyphens hyphens,
    TextMotion textMotion
)

Styling configuration for a Text.

TextStyle(
    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,
    DrawStyle drawStyle,
    TextAlign textAlign,
    TextDirection textDirection,
    @NonNull TextUnit lineHeight,
    TextIndent textIndent,
    PlatformTextStyle platformStyle,
    LineHeightStyle lineHeightStyle,
    LineBreak lineBreak,
    Hyphens hyphens,
    TextMotion textMotion
)

Styling configuration for a Text.

Public methods

final @NonNull TextStyle
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,
    DrawStyle drawStyle,
    TextAlign textAlign,
    TextDirection textDirection,
    @NonNull TextUnit lineHeight,
    TextIndent textIndent,
    PlatformTextStyle platformStyle,
    LineHeightStyle lineHeightStyle,
    LineBreak lineBreak,
    Hyphens hyphens,
    TextMotion textMotion
)
final @NonNull TextStyle
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,
    DrawStyle drawStyle,
    TextAlign textAlign,
    TextDirection textDirection,
    @NonNull TextUnit lineHeight,
    TextIndent textIndent,
    PlatformTextStyle platformStyle,
    LineHeightStyle lineHeightStyle,
    LineBreak lineBreak,
    Hyphens hyphens,
    TextMotion textMotion
)
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

The brush to use when drawing text.

final @NonNull Color

The text color.

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 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 Hyphens

The hyphens configuration of the paragraph.

final @NonNull TextUnit

The amount of space to add between each letter.

final LineBreak

The line breaking configuration of the paragraph.

final @NonNull TextUnit

Line height for the Paragraph in TextUnit unit, e.g. SP or EM.

final LineHeightStyle

The configuration for line height such as vertical alignment of the line, whether to apply additional space as a result of line height to top of first line top and bottom of last line.

final LocaleList

The locale list used to select region-specific glyphs.

final PlatformTextStyle

Platform specific TextStyle parameters.

final Shadow

The shadow effect applied on the text.

final TextAlign

The alignment of the text within the lines of the paragraph.

final TextDecoration

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

final TextDirection

The algorithm to be used to resolve the final text and paragraph direction: Left To Right or Right To Left.

final TextGeometricTransform

The geometric transformation applied the text.

final TextIndent

The indentation of the paragraph.

final TextMotion

Text character placement configuration, whether to optimize for animated or static text.

final boolean
final boolean

Returns true if text layout affecting attributes between this TextStyle and other are the same.

int
final @NonNull TextStyle

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

final @NonNull TextStyle

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

final @NonNull TextStyle

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

final @NonNull TextStyle
merge(
    @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,
    DrawStyle drawStyle,
    TextAlign textAlign,
    TextDirection textDirection,
    @NonNull TextUnit lineHeight,
    TextIndent textIndent,
    LineHeightStyle lineHeightStyle,
    LineBreak lineBreak,
    Hyphens hyphens,
    PlatformTextStyle platformStyle,
    TextMotion textMotion
)

Fast merge non-default values and parameters.

final @NonNull TextStyle

Plus operator overload that applies a merge.

final @NonNull TextStyle

Plus operator overload that applies a merge.

final @NonNull TextStyle

Plus operator overload that applies a merge.

final @NonNull ParagraphStyle
final @NonNull SpanStyle
@NonNull String

Public constructors

TextStyle

public TextStyle(
    @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,
    DrawStyle drawStyle,
    TextAlign textAlign,
    TextDirection textDirection,
    @NonNull TextUnit lineHeight,
    TextIndent textIndent,
    PlatformTextStyle platformStyle,
    LineHeightStyle lineHeightStyle,
    LineBreak lineBreak,
    Hyphens hyphens,
    TextMotion textMotion
)

Styling configuration for a Text.

import androidx.compose.material.Text
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.em
import androidx.compose.ui.unit.sp

Text(
    text = "Demo Text",
    style = TextStyle(
        color = Color.Red,
        fontSize = 16.sp,
        fontFamily = FontFamily.Monospace,
        fontWeight = FontWeight.W800,
        fontStyle = FontStyle.Italic,
        letterSpacing = 0.5.em,
        background = Color.LightGray,
        textDecoration = TextDecoration.Underline
    )
)
Parameters
@NonNull Color color

The text color.

@NonNull TextUnit fontSize

The size of glyphs to use when painting the text. This may be TextUnit.Unspecified for inheriting from another TextStyle.

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 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.

DrawStyle drawStyle

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

TextAlign textAlign

The alignment of the text within the lines of the paragraph.

TextDirection textDirection

The algorithm to be used to resolve the final text and paragraph direction: Left To Right or Right To Left. If no value is provided the system will use the LayoutDirection as the primary signal.

@NonNull TextUnit lineHeight

Line height for the Paragraph in TextUnit unit, e.g. SP or EM.

TextIndent textIndent

The indentation of the paragraph.

PlatformTextStyle platformStyle

Platform specific TextStyle parameters.

LineHeightStyle lineHeightStyle

the configuration for line height such as vertical alignment of the line, whether to apply additional space as a result of line height to top of first line top and bottom of last line. The configuration is applied only when a lineHeight is defined. When null, LineHeightStyle.Default is used.

LineBreak lineBreak

The line breaking configuration for the text.

Hyphens hyphens

The configuration of hyphenation.

TextMotion textMotion

Text character placement, whether to optimize for animated or static text.

TextStyle

public TextStyle(
    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,
    DrawStyle drawStyle,
    TextAlign textAlign,
    TextDirection textDirection,
    @NonNull TextUnit lineHeight,
    TextIndent textIndent,
    PlatformTextStyle platformStyle,
    LineHeightStyle lineHeightStyle,
    LineBreak lineBreak,
    Hyphens hyphens,
    TextMotion textMotion
)

Styling configuration for a Text.

import androidx.compose.material.Text
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.em
import androidx.compose.ui.unit.sp

Text(
    text = "Demo Text",
    style = TextStyle(
        brush = Brush.linearGradient(listOf(Color.Red, Color.Blue, Color.Green)),
        alpha = 0.8f,
        fontSize = 16.sp,
        fontFamily = FontFamily.Monospace,
        fontWeight = FontWeight.W800,
        fontStyle = FontStyle.Italic,
        letterSpacing = 0.5.em,
        textDecoration = TextDecoration.Underline
    )
)
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 to use when painting the text. This may be TextUnit.Unspecified for inheriting from another TextStyle.

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 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.

DrawStyle drawStyle

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

TextAlign textAlign

The alignment of the text within the lines of the paragraph.

TextDirection textDirection

The algorithm to be used to resolve the final text and paragraph direction: Left To Right or Right To Left. If no value is provided the system will use the LayoutDirection as the primary signal.

@NonNull TextUnit lineHeight

Line height for the Paragraph in TextUnit unit, e.g. SP or EM.

TextIndent textIndent

The indentation of the paragraph.

PlatformTextStyle platformStyle

Platform specific TextStyle parameters.

LineHeightStyle lineHeightStyle

the configuration for line height such as vertical alignment of the line, whether to apply additional space as a result of line height to top of first line top and bottom of last line. The configuration is applied only when a lineHeight is defined.

LineBreak lineBreak

The line breaking configuration for the text.

Hyphens hyphens

The configuration of hyphenation.

TextMotion textMotion

Text character placement, whether to optimize for animated or static text.

Public methods

copy

public final @NonNull TextStyle 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,
    DrawStyle drawStyle,
    TextAlign textAlign,
    TextDirection textDirection,
    @NonNull TextUnit lineHeight,
    TextIndent textIndent,
    PlatformTextStyle platformStyle,
    LineHeightStyle lineHeightStyle,
    LineBreak lineBreak,
    Hyphens hyphens,
    TextMotion textMotion
)

copy

public final @NonNull TextStyle 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,
    DrawStyle drawStyle,
    TextAlign textAlign,
    TextDirection textDirection,
    @NonNull TextUnit lineHeight,
    TextIndent textIndent,
    PlatformTextStyle platformStyle,
    LineHeightStyle lineHeightStyle,
    LineBreak lineBreak,
    Hyphens hyphens,
    TextMotion textMotion
)

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

The brush to use when drawing text. If not null, overrides color.

getColor

public final @NonNull Color getColor()

The text color.

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 to use when painting the text. This may be TextUnit.Unspecified for inheriting from another TextStyle.

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

getHyphens

public final Hyphens getHyphens()

The hyphens configuration of the paragraph.

getLetterSpacing

public final @NonNull TextUnit getLetterSpacing()

The amount of space to add between each letter.

getLineBreak

public final LineBreak getLineBreak()

The line breaking configuration of the paragraph.

getLineHeight

public final @NonNull TextUnit getLineHeight()

Line height for the Paragraph in TextUnit unit, e.g. SP or EM.

getLineHeightStyle

public final LineHeightStyle getLineHeightStyle()

The configuration for line height such as vertical alignment of the line, whether to apply additional space as a result of line height to top of first line top and bottom of last line.

The configuration is applied only when a lineHeight is defined.

When null, LineHeightStyle.Default is used.

getLocaleList

public final LocaleList getLocaleList()

The locale list used to select region-specific glyphs.

getPlatformStyle

public final PlatformTextStyle getPlatformStyle()

Platform specific TextStyle parameters.

getShadow

public final Shadow getShadow()

The shadow effect applied on the text.

getTextAlign

public final TextAlign getTextAlign()

The alignment of the text within the lines of the paragraph.

getTextDecoration

public final TextDecoration getTextDecoration()

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

getTextDirection

public final TextDirection getTextDirection()

The algorithm to be used to resolve the final text and paragraph direction: Left To Right or Right To Left. If no value is provided the system will use the LayoutDirection as the primary signal.

getTextGeometricTransform

public final TextGeometricTransform getTextGeometricTransform()

The geometric transformation applied the text.

getTextIndent

public final TextIndent getTextIndent()

The indentation of the paragraph.

getTextMotion

public final TextMotion getTextMotion()

Text character placement configuration, whether to optimize for animated or static text.

hasSameDrawAffectingAttributes

public final boolean hasSameDrawAffectingAttributes(@NonNull TextStyle other)

hasSameLayoutAffectingAttributes

public final boolean hasSameLayoutAffectingAttributes(@NonNull TextStyle other)

Returns true if text layout affecting attributes between this TextStyle and other are the same.

The attributes that do not require a layout change are color, textDecoration and shadow.

Majority of attributes change text layout, and examples are line height, font properties, font size, locale etc.

This function can be used to identify if a new text layout is required for a given TextStyle.

Parameters
@NonNull TextStyle other

The TextStyle to compare to.

hashCode

public int hashCode()

merge

public final @NonNull TextStyle merge(@NonNull ParagraphStyle other)

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

See also
merge

merge

public final @NonNull TextStyle merge(@NonNull SpanStyle other)

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

See also
merge

merge

public final @NonNull TextStyle merge(TextStyle other)

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

other text style's null or inherit properties are replaced with the non-null properties of this text 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 text style is null, returns this text style.

merge

public final @NonNull TextStyle merge(
    @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,
    DrawStyle drawStyle,
    TextAlign textAlign,
    TextDirection textDirection,
    @NonNull TextUnit lineHeight,
    TextIndent textIndent,
    LineHeightStyle lineHeightStyle,
    LineBreak lineBreak,
    Hyphens hyphens,
    PlatformTextStyle platformStyle,
    TextMotion textMotion
)

Fast merge non-default values and parameters.

This is the same algorithm as merge but does not require allocating it's parameter and may return this instead of allocating a result when all values are default.

This is a similar algorithm to copy but when either this or a parameter are set to a default value, the other value will take precedent.

To explain better, consider the following examples:

Example 1:

Example 2:

Example 3:

You should always use this method over the merge overload when you do not already have a TextStyle allocated. You should chose this over copy when building a theming system and applying styling information to a specific usage.

Returns
@NonNull TextStyle

this or a new TextLayoutResult with all parameters chosen to the non-default option provided.

See also
merge

plus

public final @NonNull TextStyle plus(@NonNull ParagraphStyle other)

Plus operator overload that applies a merge.

plus

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

Plus operator overload that applies a merge.

plus

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

Plus operator overload that applies a merge.

toParagraphStyle

public final @NonNull ParagraphStyle toParagraphStyle()

toSpanStyle

public final @NonNull SpanStyle toSpanStyle()

toString

public @NonNull String toString()