Use text for labels, titles, body copy, and inline content that should stay consistent with the surrounding theme.
import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.Composable
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
import com.composables.ui.components.Text
import com.composeunstyled.ProvideTextStyle
@Composable
fun TextExample() {
Text("Hello world")
}Installation
implementation("com.composables:ui:0.1.0")Add the required dependencies
implementation("com.composables:composeunstyled:2.7.0")
Copy and paste the following sources into your project
components/Text.kt
package com.composables.ui.components
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.isSpecified
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.TextUnit
import com.composeunstyled.LocalContentColor
import com.composeunstyled.LocalTextStyle
import com.composeunstyled.Text as UnstyledText
/**
* A styled text primitive that inherits local typography and content color.
* @param text String content displayed by the text component.
* @param modifier Modifier applied to the text.
* @param style Base text style used when drawing the text.
* @param textAlign Alignment used for the text within its bounds.
* @param lineHeight Line height used for the text.
* @param fontSize Font size used for the text.
* @param letterSpacing Letter spacing used for the text.
* @param fontWeight Font weight used for the text.
* @param color Color used to draw the text.
* @param fontFamily Font family used for the text.
* @param singleLine Whether the text is forced onto a single line.
* @param minLines Minimum number of lines reserved for the text.
* @param maxLines Maximum number of lines allowed for the text.
* @param overflow How overflowing text should be handled.
*/
@Composable
fun Text(
text: String,
modifier: Modifier = Modifier,
style: TextStyle = LocalTextStyle.current,
textAlign: TextAlign = TextAlign.Unspecified,
lineHeight: TextUnit = TextUnit.Unspecified,
fontSize: TextUnit = style.fontSize,
letterSpacing: TextUnit = style.letterSpacing,
fontWeight: FontWeight? = style.fontWeight,
color: Color = if (style.color.isSpecified) style.color else LocalContentColor.current,
fontFamily: FontFamily? = style.fontFamily,
singleLine: Boolean = false,
minLines: Int = 1,
maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE,
overflow: TextOverflow = TextOverflow.Clip,
) {
UnstyledText(
text = text,
modifier = modifier,
style = style,
textAlign = textAlign,
lineHeight = lineHeight,
fontSize = fontSize,
letterSpacing = letterSpacing,
fontWeight = fontWeight,
color = color,
fontFamily = fontFamily,
singleLine = singleLine,
minLines = minLines,
maxLines = maxLines,
overflow = overflow,
)
}components/Utils.kt
package com.composables.ui.components
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.tween
import androidx.compose.foundation.interaction.InteractionSource
import androidx.compose.foundation.interaction.collectIsFocusedAsState
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.composables.ui.theme.colors
import com.composables.ui.theme.ringColor
import com.composeunstyled.FocusRingVisibility
import com.composeunstyled.collectIsFocusVisibleAsState
import com.composeunstyled.outline
import com.composeunstyled.theme.Theme
@Composable
fun Modifier.focusRing(
interactionSource: InteractionSource,
width: Dp = 2.dp,
color: Color = Theme[colors][ringColor],
shape: Shape = RectangleShape,
offset: Dp = 0.dp,
visibility: FocusRingVisibility = FocusRingVisibility.FocusVisible,
): Modifier {
val showFocusRing by if (visibility == FocusRingVisibility.FocusVisible) {
interactionSource.collectIsFocusVisibleAsState()
} else {
interactionSource.collectIsFocusedAsState()
}
val animatedWidth by animateDpAsState(
targetValue = if (showFocusRing) width else 0.dp,
animationSpec = tween(durationMillis = 120),
label = "FocusRingWidth",
)
return this then Modifier.outline(
width = animatedWidth,
color = color,
shape = shape,
offset = offset,
)
}
@Composable
fun Modifier.bouncyPress(
interactionSource: InteractionSource,
enabled: Boolean = true,
pressedScale: Float = 0.98f,
): Modifier {
val pressed by interactionSource.collectIsPressedAsState()
val scale by animateFloatAsState(
targetValue = if (enabled && pressed) pressedScale else 1f,
animationSpec = spring(
dampingRatio = Spring.DampingRatioMediumBouncy,
stiffness = Spring.StiffnessMediumLow,
),
label = "BouncyPressScale",
)
return this then Modifier.graphicsLayer {
scaleX = scale
scaleY = scale
}
}Examples
Styled
import androidx.compose.runtime.Composable
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
import com.composables.ui.components.Text
@Composable
fun StyledTextExample() {
Text(
text = "Styled headline",
fontSize = 24.sp,
lineHeight = 32.sp,
fontWeight = FontWeight.SemiBold,
)
}Themed
import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.Composable
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
import com.composables.ui.components.Text
import com.composeunstyled.ProvideTextStyle
@Composable
fun ThemedTextExample() {
ProvideTextStyle(
TextStyle(
fontSize = 18.sp,
lineHeight = 28.sp,
fontWeight = FontWeight.Medium,
),
) {
Column {
Text("This text is themed")
Text("And so is this one")
}
}
}API Reference
Text
A styled text primitive that inherits local typography and content color.
@Composable
fun Text(
text: String,
modifier: Modifier = Modifier,
style: TextStyle = LocalTextStyle.current,
textAlign: TextAlign = TextAlign.Unspecified,
lineHeight: TextUnit = TextUnit.Unspecified,
fontSize: TextUnit = style.fontSize,
letterSpacing: TextUnit = style.letterSpacing,
fontWeight: FontWeight? = style.fontWeight,
color: Color = if (style.color.isSpecified) style.color else LocalContentColor.current,
fontFamily: FontFamily? = style.fontFamily,
singleLine: Boolean = false,
minLines: Int = 1,
maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE,
overflow: TextOverflow = TextOverflow.Clip,
)
| Parameter | Type | Description |
|---|---|---|
text |
String |
String content displayed by the text component. |
modifier |
Modifier |
Modifier applied to the text. |
style |
TextStyle |
Base text style used when drawing the text. |
textAlign |
TextAlign |
Alignment used for the text within its bounds. |
lineHeight |
TextUnit |
Line height used for the text. |
fontSize |
TextUnit |
Font size used for the text. |
letterSpacing |
TextUnit |
Letter spacing used for the text. |
fontWeight |
FontWeight? |
Font weight used for the text. |
color |
Color |
Color used to draw the text. |
fontFamily |
FontFamily? |
Font family used for the text. |
singleLine |
Boolean |
Whether the text is forced onto a single line. |
minLines |
Int |
Minimum number of lines reserved for the text. |
maxLines |
Int |
Maximum number of lines allowed for the text. |
overflow |
TextOverflow |
How overflowing text should be handled. |