androidx.compose.material3

Text

Composable function

Text shows text. Pick a style from the theme with MaterialTheme.typography, instead of setting sizes by hand.

Text with a theme style
Text with a theme style
Column {
    Text("Headline", style = MaterialTheme.typography.headlineSmall)
    Text("Body text in the default style.")
    Text("Label", style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.primary)
}

The Material Text uses the theme: its color comes from the surrounding component (LocalContentColor), and its style from LocalTextStyle. Inside a Button, it gets the button's text color automatically.

String

Plain text.

@Composable
fun Text(
    text: String,
    modifier: Modifier = Modifier,
    color: Color = Color.Unspecified,
    autoSize: TextAutoSize? = null,
    fontSize: TextUnit = TextUnit.Unspecified,
    fontStyle: FontStyle? = null,
    fontWeight: FontWeight? = null,
    fontFamily: FontFamily? = null,
    letterSpacing: TextUnit = TextUnit.Unspecified,
    textDecoration: TextDecoration? = null,
    textAlign: TextAlign? = null,
    lineHeight: TextUnit = TextUnit.Unspecified,
    overflow: TextOverflow = TextOverflow.Clip,
    softWrap: Boolean = true,
    maxLines: Int = Int.MAX_VALUE,
    minLines: Int = 1,
    onTextLayout: ((TextLayoutResult) -> Unit)? = null,
    style: TextStyle = LocalTextStyle.current,
)

text: StringRequired

The text to show. A String, or an AnnotatedString for mixed styles.

Different styles in one text
Different styles in one text
Text(
    buildAnnotatedString {
        append("Delivery ")
        withStyle(SpanStyle(fontWeight = FontWeight.Bold)) { append("tomorrow") }
        append(", before ")
        withStyle(SpanStyle(color = MaterialTheme.colorScheme.primary)) { append("12:00") }
    },
)

modifier: ModifierOptional

Default: Modifier

Changes size, padding, or position of the text. See the Modifier guide.

color: ColorOptional

Default: Color.Unspecified

The text color. Default: taken from style, or else from the surrounding component. In the "changing color" forms, pass a function that returns the color. This avoids recomposing while the color animates.

autoSize: TextAutoSize?OptionalSince 1.9

Default: null

Makes the text shrink or grow to fit its space, for example TextAutoSize.StepBased().

fontSize: TextUnitOptional

Default: TextUnit.Unspecified

The size of the text, in sp. Prefer a style from the theme.

fontStyle: FontStyle?Optional

fontWeight: FontWeight?Optional

Default: null

How bold the text is, for example FontWeight.Bold.

fontFamily: FontFamily?Optional

Default: null

The typeface. Default: the theme's font.

letterSpacing: TextUnitOptional

Default: TextUnit.Unspecified

Extra space between letters.

textDecoration: TextDecoration?Optional

textAlign: TextAlign?Optional

Default: null

How lines are aligned: start, center, end or justify. Only visible when the text is wider than one line or has a set width.

Centered text
Centered text
Text(
    "Centered text that wraps onto a second line.",
    textAlign = TextAlign.Center,
    modifier = Modifier.width(220.dp),
)

lineHeight: TextUnitOptional

Default: TextUnit.Unspecified

The height of each line.

overflow: TextOverflowOptional

Default: TextOverflow.Clip

What to do with text that does not fit. TextOverflow.Ellipsis ends it with "…". Default: cut off.

Cut off long text with an ellipsis
Cut off long text with an ellipsis
Text(
    "This sentence is too long to fit on one line in this narrow space.",
    maxLines = 1,
    overflow = TextOverflow.Ellipsis,
    modifier = Modifier.width(240.dp),
)

softWrap: BooleanOptional

Default: true

Set to false to keep the text on one line, even if it does not fit.

maxLines: IntOptional

Default: Int.MAX_VALUE

The most lines to show. Combine with overflow.

minLines: IntOptional

Default: 1

The fewest lines to take up, even if the text is shorter.

onTextLayout: ((TextLayoutResult) -> Unit)?Optional

Default: null

Called with the text layout after it is measured, for example to find out if the text was cut off.

style: TextStyleOptional

Default: LocalTextStyle.current

A whole text style: font, size, weight, line height. Use one from MaterialTheme.typography, like titleMedium. Single parameters like fontSize override parts of it.

String, changing color

Takes color as a function, for colors that animate often.

@Composable
fun Text(
    text: String,
    color: ColorProducer,
    modifier: Modifier = Modifier,
    autoSize: TextAutoSize? = null,
    fontSize: TextUnit = TextUnit.Unspecified,
    fontStyle: FontStyle? = null,
    fontWeight: FontWeight? = null,
    fontFamily: FontFamily? = null,
    letterSpacing: TextUnit = TextUnit.Unspecified,
    textDecoration: TextDecoration? = null,
    textAlign: TextAlign? = null,
    lineHeight: TextUnit = TextUnit.Unspecified,
    overflow: TextOverflow = TextOverflow.Clip,
    softWrap: Boolean = true,
    maxLines: Int = Int.MAX_VALUE,
    minLines: Int = 1,
    onTextLayout: ((TextLayoutResult) -> Unit)? = null,
    style: TextStyle = LocalTextStyle.current,
)

color: ColorProducerRequired

The text color. Default: taken from style, or else from the surrounding component. In the "changing color" forms, pass a function that returns the color. This avoids recomposing while the color animates.

Same as above: text, modifier, autoSize, fontSize, fontStyle, fontWeight, fontFamily, letterSpacing, textDecoration, textAlign, lineHeight, overflow, softWrap, maxLines, minLines, onTextLayout, style.

Styled text

Takes an AnnotatedString, with different styles in one text.

@Composable
fun Text(
    text: AnnotatedString,
    modifier: Modifier = Modifier,
    color: Color = Color.Unspecified,
    autoSize: TextAutoSize? = null,
    fontSize: TextUnit = TextUnit.Unspecified,
    fontStyle: FontStyle? = null,
    fontWeight: FontWeight? = null,
    fontFamily: FontFamily? = null,
    letterSpacing: TextUnit = TextUnit.Unspecified,
    textDecoration: TextDecoration? = null,
    textAlign: TextAlign? = null,
    lineHeight: TextUnit = TextUnit.Unspecified,
    overflow: TextOverflow = TextOverflow.Clip,
    softWrap: Boolean = true,
    maxLines: Int = Int.MAX_VALUE,
    minLines: Int = 1,
    inlineContent: Map<String, InlineTextContent> = mapOf(),
    onTextLayout: (TextLayoutResult) -> Unit = {},
    style: TextStyle = LocalTextStyle.current,
)

text: AnnotatedStringRequired

The text to show. A String, or an AnnotatedString for mixed styles.

Different styles in one text
Different styles in one text
Text(
    buildAnnotatedString {
        append("Delivery ")
        withStyle(SpanStyle(fontWeight = FontWeight.Bold)) { append("tomorrow") }
        append(", before ")
        withStyle(SpanStyle(color = MaterialTheme.colorScheme.primary)) { append("12:00") }
    },
)

inlineContent: Map<String, InlineTextContent>Optional

Default: mapOf()

Composables placed inside the text, like small icons. Used with appendInlineContent in an AnnotatedString.

onTextLayout: (TextLayoutResult) -> UnitOptional

Default: {}

Called with the text layout after it is measured, for example to find out if the text was cut off.

Same as above: modifier, color, autoSize, fontSize, fontStyle, fontWeight, fontFamily, letterSpacing, textDecoration, textAlign, lineHeight, overflow, softWrap, maxLines, minLines, style.

Styled text, changing color

AnnotatedString with color as a function.

@Composable
fun Text(
    text: AnnotatedString,
    color: ColorProducer,
    modifier: Modifier = Modifier,
    autoSize: TextAutoSize? = null,
    fontSize: TextUnit = TextUnit.Unspecified,
    fontStyle: FontStyle? = null,
    fontWeight: FontWeight? = null,
    fontFamily: FontFamily? = null,
    letterSpacing: TextUnit = TextUnit.Unspecified,
    textDecoration: TextDecoration? = null,
    textAlign: TextAlign? = null,
    lineHeight: TextUnit = TextUnit.Unspecified,
    overflow: TextOverflow = TextOverflow.Clip,
    softWrap: Boolean = true,
    maxLines: Int = Int.MAX_VALUE,
    minLines: Int = 1,
    inlineContent: Map<String, InlineTextContent> = mapOf(),
    onTextLayout: (TextLayoutResult) -> Unit = {},
    style: TextStyle = LocalTextStyle.current,
)

Same as above: text, color, modifier, autoSize, fontSize, fontStyle, fontWeight, fontFamily, letterSpacing, textDecoration, textAlign, lineHeight, overflow, softWrap, maxLines, minLines, inlineContent, onTextLayout, style.