State of Compose 2023 results are in! Click here to learn more

← Back to Foundation

Text

Component
in
Foundation
. Since 0.1.0-dev15

Overview

Code Examples

Video

High level element that displays text and provides semantics / accessibility information.

The default style uses the AmbientTextStyle defined by a theme. If you are setting your own style, you may want to consider first retrieving AmbientTextStyle, and using TextStyle.copy to keep any theme defined attributes, only modifying the specific attributes you want to override.

For ease of use, commonly used parameters from TextStyle are also present here. The order of precedence is as follows:

  • If a parameter is explicitly set here (i.e, it is not null or TextUnit.Unspecified), then this parameter will always be used.
  • If a parameter is not set, (null or TextUnit.Unspecified), then the corresponding value from style will be used instead.

Additionally, for color, if color is not set, and style does not have a color, then AmbientContentColor will be used - this allows this Text or element containing this Text to adapt to different background colors and still maintain contrast and accessibility.

@param text The text to be displayed. @param modifier Modifier to apply to this layout node. @param color Color to apply to the text. If Color.Unspecified, and style has no color set, this will be AmbientContentColor. @param fontSize The size of glyphs to use when painting the text. See TextStyle.fontSize. @param fontStyle The typeface variant to use when drawing the letters (e.g., italic). See TextStyle.fontStyle. @param fontWeight The typeface thickness to use when painting the text (e.g., FontWeight.Bold). @param fontFamily The font family to be used when rendering the text. See TextStyle.fontFamily. @param letterSpacing The amount of space to add between each letter. See TextStyle.letterSpacing. @param textDecoration The decorations to paint on the text (e.g., an underline). See TextStyle.textDecoration. @param textAlign The alignment of the text within the lines of the paragraph. See TextStyle.textAlign. @param lineHeight Line height for the Paragraph in TextUnit unit, e.g. SP or EM. See TextStyle.lineHeight. @param overflow How visual overflow should be handled. @param softWrap Whether the text should break at soft line breaks. If false, the glyphs in the text will be positioned as if there was unlimited horizontal space. If softWrap is false, overflow and TextAlign may have unexpected effects. @param maxLines An optional maximum number of lines for the text to span, wrapping if necessary. If the text exceeds the given number of lines, it will be truncated according to overflow and softWrap. If it is not null, then it must be greater than zero. @param onTextLayout Callback that is executed when a new text layout is calculated. @param style Style configuration for the text such as color, font, line height etc.

Overloads

Text

@Deprecated(
    message = "Use androidx.compose.material.Text for a high level Text component that " +
        "consumes theming information, or androidx.compose.foundation.text.BasicText for a basic " +
        "unopinionated component that does not have default theming",
    replaceWith = ReplaceWith(
        "Text(text, modifier, color, fontSize, fontStyle, fontWeight, fontFamily, " +
            "letterSpacing, textDecoration, textAlign, lineHeight, overflow, softWrap, maxLines, " +
            "onTextLayout, style)",
        "androidx.compose.material.Text"
    )
)
@Composable
fun Text(
    text: String,
    modifier: Modifier = Modifier,
    color: Color = Color.Unspecified,
    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,
    onTextLayout: (TextLayoutResult) -> Unit = {},
    style: TextStyle = AmbientTextStyle.current
)

Parameters

NameDescription
textThe text to be displayed.
modifierModifier to apply to this layout node.
colorColor to apply to the text. If Color.Unspecified, and style has no color set, this will be AmbientContentColor.
fontSizeThe size of glyphs to use when painting the text. See TextStyle.fontSize.
fontStyleThe typeface variant to use when drawing the letters (e.g., italic). See TextStyle.fontStyle.
fontWeightThe typeface thickness to use when painting the text (e.g., FontWeight.Bold).
fontFamilyThe font family to be used when rendering the text. See TextStyle.fontFamily.
letterSpacingThe amount of space to add between each letter. See TextStyle.letterSpacing.
textDecorationThe decorations to paint on the text (e.g., an underline). See TextStyle.textDecoration.
textAlignThe alignment of the text within the lines of the paragraph. See TextStyle.textAlign.
lineHeightLine height for the Paragraph in TextUnit unit, e.g. SP or EM. See TextStyle.lineHeight.
overflowHow visual overflow should be handled.
softWrapWhether the text should break at soft line breaks. If false, the glyphs in the text will be positioned as if there was unlimited horizontal space. If softWrap is false, overflow and TextAlign may have unexpected effects.
maxLinesAn optional maximum number of lines for the text to span, wrapping if necessary. If the text exceeds the given number of lines, it will be truncated according to overflow and softWrap. If it is not null, then it must be greater than zero.
onTextLayoutCallback that is executed when a new text layout is calculated.
styleStyle configuration for the text such as color, font, line height etc

Text

@Deprecated(
    message = "Use androidx.compose.material.Text for a high level Text component that " +
        "consumes theming information, or androidx.compose.foundation.text.BasicText for a basic " +
        "unopinionated component that does not have default theming",
    replaceWith = ReplaceWith(
        "Text(text, modifier, color, fontSize, fontStyle, fontWeight, fontFamily, " +
            "letterSpacing, textDecoration, textAlign, lineHeight, overflow, softWrap, maxLines, " +
            "inlineContent, onTextLayout, style)",
        "androidx.compose.material.Text"
    )
)
@Composable
fun Text(
    text: AnnotatedString,
    modifier: Modifier = Modifier,
    color: Color = Color.Unspecified,
    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,
    inlineContent: Map<String, InlineTextContent> = mapOf(),
    onTextLayout: (TextLayoutResult) -> Unit = {},
    style: TextStyle = AmbientTextStyle.current
)

Parameters

NameDescription
textThe text to be displayed.
modifierModifier to apply to this layout node.
colorColor to apply to the text. If Color.Unspecified, and style has no color set, this will be AmbientContentColor.
fontSizeThe size of glyphs to use when painting the text. See TextStyle.fontSize.
fontStyleThe typeface variant to use when drawing the letters (e.g., italic). See TextStyle.fontStyle.
fontWeightThe typeface thickness to use when painting the text (e.g., FontWeight.Bold).
fontFamilyThe font family to be used when rendering the text. See TextStyle.fontFamily.
letterSpacingThe amount of space to add between each letter. See TextStyle.letterSpacing.
textDecorationThe decorations to paint on the text (e.g., an underline). See TextStyle.textDecoration.
textAlignThe alignment of the text within the lines of the paragraph. See TextStyle.textAlign.
lineHeightLine height for the Paragraph in TextUnit unit, e.g. SP or EM. See TextStyle.lineHeight.
overflowHow visual overflow should be handled.
softWrapWhether the text should break at soft line breaks. If false, the glyphs in the text will be positioned as if there was unlimited horizontal space. If softWrap is false, overflow and TextAlign may have unexpected effects.
maxLinesAn optional maximum number of lines for the text to span, wrapping if necessary. If the text exceeds the given number of lines, it will be truncated according to overflow and softWrap. If it is not null, then it must be greater than zero.
inlineContentA map store composables that replaces certain ranges of the text. It's used to insert composables into text layout. Check InlineTextContent for more information.
onTextLayoutCallback that is executed when a new text layout is calculated.
styleStyle configuration for the text such as color, font, line height etc