Text
Composable Component
High level element that displays text and provides semantics / accessibility information.
Common
@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,
)
Parameters
text | the text to be displayed |
modifier | the Modifier to be applied to this layout node |
color | Color to apply to the text. If Color.Unspecified , and style has no color set, this will be LocalContentColor . |
autoSize | Enable auto sizing for this text composable. Finds the biggest font size that fits in the available space and lays the text out with this size. This performs multiple layout passes and can be slower than using a fixed font size. This takes precedence over sizes defined through fontSize and style . See TextAutoSize . |
fontSize | the size of glyphs to use when painting the text. See TextStyle.fontSize . |
fontStyle | the typeface variant to use when drawing the letters (e.g., italic). See TextStyle.fontStyle . |
fontWeight | the typeface thickness to use when painting the text (e.g., FontWeight.Bold ). |
fontFamily | the font family to be used when rendering the text. See TextStyle.fontFamily . |
letterSpacing | the amount of space to add between each letter. See TextStyle.letterSpacing . |
textDecoration | the decorations to paint on the text (e.g., an underline). See TextStyle.textDecoration . |
textAlign | the alignment of the text within the lines of the paragraph. See TextStyle.textAlign . |
lineHeight | line height for the Paragraph in TextUnit unit, e.g. SP or EM. See TextStyle.lineHeight . |
overflow | how visual overflow should be handled. |
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. |
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 . |
minLines | The minimum height in terms of minimum number of visible lines. It is required that 1 <= minLines <= maxLines . |
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. |
style | style configuration for the text such as color, font, line height etc. |
Common
@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,
)
Parameters
text | the text to be displayed |
modifier | the Modifier to be applied to this layout node |
color | Color to apply to the text. If Color.Unspecified , and style has no color set, this will be LocalContentColor . |
autoSize | Enable auto sizing for this text composable. Finds the biggest font size that fits in the available space and lays the text out with this size. This performs multiple layout passes and can be slower than using a fixed font size. This takes precedence over sizes defined through fontSize and style . See TextAutoSize . |
fontSize | the size of glyphs to use when painting the text. See TextStyle.fontSize . |
fontStyle | the typeface variant to use when drawing the letters (e.g., italic). See TextStyle.fontStyle . |
fontWeight | the typeface thickness to use when painting the text (e.g., FontWeight.Bold ). |
fontFamily | the font family to be used when rendering the text. See TextStyle.fontFamily . |
letterSpacing | the amount of space to add between each letter. See TextStyle.letterSpacing . |
textDecoration | the decorations to paint on the text (e.g., an underline). See TextStyle.textDecoration . |
textAlign | the alignment of the text within the lines of the paragraph. See TextStyle.textAlign . |
lineHeight | line height for the Paragraph in TextUnit unit, e.g. SP or EM. See TextStyle.lineHeight . |
overflow | how visual overflow should be handled. |
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. |
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 . |
minLines | The minimum height in terms of minimum number of visible lines. It is required that 1 <= minLines <= maxLines . |
inlineContent | a map storing composables that replaces certain ranges of the text, used to insert composables into text layout. See InlineTextContent . |
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. |
style | style configuration for the text such as color, font, line height etc. |
Common
Deprecated Maintained for binary compatibility. Use version with autoSize instead
@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,
minLines: Int = 1,
onTextLayout: ((TextLayoutResult) -> Unit)? = null,
style: TextStyle = LocalTextStyle.current,
) =
Text(
text = text,
modifier = modifier,
color = color,
fontSize = fontSize,
fontStyle = fontStyle,
fontWeight = fontWeight,
fontFamily = fontFamily,
letterSpacing = letterSpacing,
textDecoration = textDecoration,
textAlign = textAlign,
lineHeight = lineHeight,
overflow = overflow,
softWrap = softWrap,
maxLines = maxLines,
minLines = minLines,
onTextLayout = onTextLayout,
style = style,
autoSize = null,
)
Common
Deprecated Maintained for binary compatibility. Use version with autoSize instead
@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,
minLines: Int = 1,
inlineContent: Map<String, InlineTextContent> = mapOf(),
onTextLayout: (TextLayoutResult) -> Unit = {},
style: TextStyle = LocalTextStyle.current,
) =
Text(
text = text,
modifier = modifier,
color = color,
fontSize = fontSize,
fontStyle = fontStyle,
fontWeight = fontWeight,
fontFamily = fontFamily,
letterSpacing = letterSpacing,
textDecoration = textDecoration,
textAlign = textAlign,
lineHeight = lineHeight,
overflow = overflow,
softWrap = softWrap,
maxLines = maxLines,
minLines = minLines,
inlineContent = inlineContent,
onTextLayout = onTextLayout,
style = style,
autoSize = null,
)
Common
Deprecated Maintained for binary compatibility. Use version with minLines instead
@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 = LocalTextStyle.current,
)
Common
Deprecated Maintained for binary compatibility. Use version with minLines instead
@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 = LocalTextStyle.current,
)
Code Examples
TextWithLinks
@Preview(showBackground = true)
@Composable
fun TextWithLinks() {
val url = "https://developer.android.com/jetpack/compose"
val annotatedString = buildAnnotatedString {
append("Build better apps faster with ")
// If the annotation's TextLinkStyles is null,
// then the link style defaults to Material styling.
withLink(LinkAnnotation.Url(url = url)) { append("Jetpack Compose") }
}
Text(annotatedString)
}