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

TextKt

public final class TextKt


Summary

Public methods

static final void
@Composable
ProvideTextStyle(
    @NonNull TextStyle value,
    @Composable @NonNull Function0<Unit> content
)

This function is used to set the current value of LocalTextStyle, merging the given style with the current style values for any missing attributes.

static final void
@Composable
Text(
    @NonNull String text,
    @NonNull Modifier modifier,
    @NonNull Color color,
    @NonNull TextUnit fontSize,
    FontStyle fontStyle,
    FontWeight fontWeight,
    FontFamily fontFamily,
    @NonNull TextUnit letterSpacing,
    TextDecoration textDecoration,
    TextAlign textAlign,
    @NonNull TextUnit lineHeight,
    @NonNull TextOverflow overflow,
    boolean softWrap,
    int maxLines,
    int minLines,
    Function1<@NonNull TextLayoutResultUnit> onTextLayout,
    @NonNull TextStyle style
)

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

static final void
@Composable
Text(
    @NonNull AnnotatedString text,
    @NonNull Modifier modifier,
    @NonNull Color color,
    @NonNull TextUnit fontSize,
    FontStyle fontStyle,
    FontWeight fontWeight,
    FontFamily fontFamily,
    @NonNull TextUnit letterSpacing,
    TextDecoration textDecoration,
    TextAlign textAlign,
    @NonNull TextUnit lineHeight,
    @NonNull TextOverflow overflow,
    boolean softWrap,
    int maxLines,
    int minLines,
    @NonNull Map<@NonNull String, @NonNull InlineTextContent> inlineContent,
    @NonNull Function1<@NonNull TextLayoutResultUnit> onTextLayout,
    @NonNull TextStyle style
)

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

static final @NonNull ProvidableCompositionLocal<@NonNull TextStyle>

CompositionLocal containing the preferred TextStyle that will be used by Text components by default.

Public methods

ProvideTextStyle

@Composable
public static final void ProvideTextStyle(
    @NonNull TextStyle value,
    @Composable @NonNull Function0<Unit> content
)

This function is used to set the current value of LocalTextStyle, merging the given style with the current style values for any missing attributes. Any Text components included in this component's content will be styled with this style unless styled explicitly.

See also
LocalTextStyle

Text

@Composable
public static final void Text(
    @NonNull String text,
    @NonNull Modifier modifier,
    @NonNull Color color,
    @NonNull TextUnit fontSize,
    FontStyle fontStyle,
    FontWeight fontWeight,
    FontFamily fontFamily,
    @NonNull TextUnit letterSpacing,
    TextDecoration textDecoration,
    TextAlign textAlign,
    @NonNull TextUnit lineHeight,
    @NonNull TextOverflow overflow,
    boolean softWrap,
    int maxLines,
    int minLines,
    Function1<@NonNull TextLayoutResultUnit> onTextLayout,
    @NonNull TextStyle style
)

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

The default style uses the LocalTextStyle provided by the MaterialTheme / components. If you are setting your own style, you may want to consider first retrieving LocalTextStyle, 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:

Additionally, for color, if color is not set, and style does not have a color, then LocalContentColor will be used.

Parameters
@NonNull String text

the text to be displayed

@NonNull Modifier modifier

the Modifier to be applied to this layout node

@NonNull Color color

Color to apply to the text. If Color.Unspecified, and style has no color set, this will be LocalContentColor.

@NonNull TextUnit fontSize

the size of glyphs to use when painting the text. See TextStyle.fontSize.

FontStyle fontStyle

the typeface variant to use when drawing the letters (e.g., italic). See TextStyle.fontStyle.

FontWeight fontWeight

the typeface thickness to use when painting the text (e.g., FontWeight.Bold).

FontFamily fontFamily

the font family to be used when rendering the text. See TextStyle.fontFamily.

@NonNull TextUnit letterSpacing

the amount of space to add between each letter. See TextStyle.letterSpacing.

TextDecoration textDecoration

the decorations to paint on the text (e.g., an underline). See TextStyle.textDecoration.

TextAlign textAlign

the alignment of the text within the lines of the paragraph. See TextStyle.textAlign.

@NonNull TextUnit lineHeight

line height for the Paragraph in TextUnit unit, e.g. SP or EM. See TextStyle.lineHeight.

@NonNull TextOverflow overflow

how visual overflow should be handled.

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

int 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. It is required that 1 <= minLines<= maxLines.

int minLines

The minimum height in terms of minimum number of visible lines. It is required that 1 <= minLines<= maxLines.

Function1<@NonNull TextLayoutResultUnit> onTextLayout

callback that is executed when a new text layout is calculated. A TextLayoutResult object that callback provides contains paragraph information, size of the text, baselines and other details. The callback can be used to add additional decoration or functionality to the text. For example, to draw selection around the text.

@NonNull TextStyle style

style configuration for the text such as color, font, line height etc.

Text

@Composable
public static final void Text(
    @NonNull AnnotatedString text,
    @NonNull Modifier modifier,
    @NonNull Color color,
    @NonNull TextUnit fontSize,
    FontStyle fontStyle,
    FontWeight fontWeight,
    FontFamily fontFamily,
    @NonNull TextUnit letterSpacing,
    TextDecoration textDecoration,
    TextAlign textAlign,
    @NonNull TextUnit lineHeight,
    @NonNull TextOverflow overflow,
    boolean softWrap,
    int maxLines,
    int minLines,
    @NonNull Map<@NonNull String, @NonNull InlineTextContent> inlineContent,
    @NonNull Function1<@NonNull TextLayoutResultUnit> onTextLayout,
    @NonNull TextStyle style
)

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

The default style uses the LocalTextStyle provided by the MaterialTheme / components. If you are setting your own style, you may want to consider first retrieving LocalTextStyle, 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:

Additionally, for color, if color is not set, and style does not have a color, then LocalContentColor will be used.

Parameters
@NonNull AnnotatedString text

the text to be displayed

@NonNull Modifier modifier

the Modifier to be applied to this layout node

@NonNull Color color

Color to apply to the text. If Color.Unspecified, and style has no color set, this will be LocalContentColor.

@NonNull TextUnit fontSize

the size of glyphs to use when painting the text. See TextStyle.fontSize.

FontStyle fontStyle

the typeface variant to use when drawing the letters (e.g., italic). See TextStyle.fontStyle.

FontWeight fontWeight

the typeface thickness to use when painting the text (e.g., FontWeight.Bold).

FontFamily fontFamily

the font family to be used when rendering the text. See TextStyle.fontFamily.

@NonNull TextUnit letterSpacing

the amount of space to add between each letter. See TextStyle.letterSpacing.

TextDecoration textDecoration

the decorations to paint on the text (e.g., an underline). See TextStyle.textDecoration.

TextAlign textAlign

the alignment of the text within the lines of the paragraph. See TextStyle.textAlign.

@NonNull TextUnit lineHeight

line height for the Paragraph in TextUnit unit, e.g. SP or EM. See TextStyle.lineHeight.

@NonNull TextOverflow overflow

how visual overflow should be handled.

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

int 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. It is required that 1 <= minLines<= maxLines.

int minLines

The minimum height in terms of minimum number of visible lines. It is required that 1 <= minLines<= maxLines.

@NonNull Map<@NonNull String, @NonNull InlineTextContent> inlineContent

a map storing composables that replaces certain ranges of the text, used to insert composables into text layout. See InlineTextContent.

@NonNull Function1<@NonNull TextLayoutResultUnit> onTextLayout

callback that is executed when a new text layout is calculated. A TextLayoutResult object that callback provides contains paragraph information, size of the text, baselines and other details. The callback can be used to add additional decoration or functionality to the text. For example, to draw selection around the text.

@NonNull TextStyle style

style configuration for the text such as color, font, line height etc.

getLocalTextStyle

public static final @NonNull ProvidableCompositionLocal<@NonNull TextStylegetLocalTextStyle()

CompositionLocal containing the preferred TextStyle that will be used by Text components by default. To set the value for this CompositionLocal, see ProvideTextStyle which will merge any missing TextStyle properties with the existing TextStyle set in this CompositionLocal.

See also
ProvideTextStyle