KPText

fun KPText(text: String, modifier: Modifier = Modifier, color: Color = Color.Unspecified, fontSize: TextUnit = TextUnit.Unspecified, fontStyle: FontStyle? = null, fontWeight: FontWeight? = null, fontFamily: FontFamily? = null, letterSpacing: TextUnit = KPTextDefaults.letterSpacing, 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 = {}, style: TextStyle = LocalTextStyle.current)

A customized text composable that wraps androidx.compose.material.Text with modified default values for letterSpacing and style. This composable ensures a more consistent typography design by overriding specific defaults, such as removing font padding.

Parameters

text

The text content to display, provided as a String.

modifier

Modifier for customizing the layout or adding behavior to the text. Default is Modifier.

color

The color of the text. Default is Color.Unspecified, which defers to the style parameter.

fontSize

The size of the text, specified as a TextUnit. Default is TextUnit.Unspecified, which defers to the style parameter.

fontStyle

The style of the text (e.g., italic). Default is null, which defers to the style parameter.

fontWeight

The weight of the text (e.g., bold). Default is null, which defers to the style parameter.

fontFamily

The font family for the text. Default is null, which defers to the style parameter.

letterSpacing

The amount of space to add between each letter. Default is KPTextDefaults.letterSpacing.

textDecoration

Decorations such as underline or strikethrough. Default is null.

textAlign

Alignment of the text within its container (e.g., start, center). Default is null.

lineHeight

The height of each line of text, specified as a TextUnit. Default is TextUnit.Unspecified.

overflow

Behavior for handling text overflow (e.g., ellipsis, clip). Default is TextOverflow.Clip.

softWrap

Whether the text should wrap when it reaches the container's bounds. Default is true.

maxLines

The maximum number of lines for the text. Default is Int.MAX_VALUE.

minLines

The minimum number of lines for the text. Default is 1.

onTextLayout

Callback invoked with the TextLayoutResult after the text is laid out. Default is an empty lambda.

style

TextStyle configuration for the text, such as color, font, and line height. This overrides platformStyle to remove the default font padding. Default is LocalTextStyle.current.

Example usage:

KPText(
text = "Hello, KP Design!",
)

See also


fun KPText(text: AnnotatedString, modifier: Modifier = Modifier, color: Color = Color.Unspecified, fontSize: TextUnit = TextUnit.Unspecified, fontStyle: FontStyle? = null, fontWeight: FontWeight? = null, fontFamily: FontFamily? = null, letterSpacing: TextUnit = KPTextDefaults.letterSpacing, 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)

A customized text composable that wraps androidx.compose.material.Text, allowing for rich text formatting using AnnotatedString. This composable adjusts default values for letterSpacing and style, ensuring consistent typography and removing default font padding.

Parameters

text

The text content to display, provided as an AnnotatedString. It allows rich text formatting, such as styled spans or inline content.

modifier

Modifier for customizing the layout or adding behavior to the text. Default is Modifier.

color

The color of the text. Default is Color.Unspecified, which defers to the style parameter.

fontSize

The size of the text, specified as a TextUnit. Default is TextUnit.Unspecified, which defers to the style parameter.

fontStyle

The style of the text (e.g., italic). Default is null, which defers to the style parameter.

fontWeight

The weight of the text (e.g., bold). Default is null, which defers to the style parameter.

fontFamily

The font family for the text. Default is null, which defers to the style parameter.

letterSpacing

The amount of space to add between each letter. Default is KPTextDefaults.letterSpacing.

textDecoration

Decorations such as underline or strikethrough. Default is null.

textAlign

Alignment of the text within its container (e.g., start, center). Default is null.

lineHeight

The height of each line of text, specified as a TextUnit. Default is TextUnit.Unspecified.

overflow

Behavior for handling text overflow (e.g., ellipsis, clip). Default is TextOverflow.Clip.

softWrap

Whether the text should wrap when it reaches the container's bounds. Default is true.

maxLines

The maximum number of lines for the text. Default is Int.MAX_VALUE.

minLines

The minimum number of lines for the text. Default is 1.

inlineContent

A map of InlineTextContent identified by keys within the AnnotatedString. This allows embedding custom composables inline with the text. Default is an empty map.

onTextLayout

Callback invoked with the TextLayoutResult after the text is laid out. Default is an empty lambda.

style

TextStyle configuration for the text, such as color, font, and line height. This overrides platformStyle to remove the default font padding. Default is LocalTextStyle.current.

Example usage:

val annotatedString = buildAnnotatedString {
append("Hello, ")
withStyle(style = SpanStyle(fontWeight = FontWeight.Bold)) {
append("KP Design!")
}
}

KPText(
text = annotatedString,
)

See also