The Text component displays strings with various typography styles, colors, and formatting options. It’s built on Compose’s BasicText and integrates with Lumo UI’s theming system.
Usage
import com.nomanr.lumo.ui.components.Text
Text(text = "Hello, World!")
Text(
text = "Styled text",
style = AppTheme.typography.h1,
color = AppTheme.colors.primary
)
Typography Styles
Lumo UI provides a comprehensive typography system:
Headings
// Large heading
Text(
text = "H1 Heading",
style = AppTheme.typography.h1
)
// Medium heading
Text(
text = "H2 Heading",
style = AppTheme.typography.h2
)
// Small heading
Text(
text = "H3 Heading",
style = AppTheme.typography.h3
)
// Extra small heading
Text(
text = "H4 Heading",
style = AppTheme.typography.h4
)
Body Text
// Primary body text
Text(
text = "This is body1 text for paragraphs.",
style = AppTheme.typography.body1
)
// Secondary body text
Text(
text = "This is body2 text for descriptions.",
style = AppTheme.typography.body2
)
// Small body text
Text(
text = "This is body3 text for fine print.",
style = AppTheme.typography.body3
)
Labels
// Large label
Text(
text = "Form Label",
style = AppTheme.typography.label1
)
// Medium label
Text(
text = "Secondary Info",
style = AppTheme.typography.label2
)
// Small label
Text(
text = "Tiny Details",
style = AppTheme.typography.label3
)
Button Text
Text(
text = "BUTTON TEXT",
style = AppTheme.typography.button
)
Text Formatting
Colors
// Theme colors
Text(
text = "Primary color text",
color = AppTheme.colors.primary
)
Text(
text = "Custom color text",
color = Color(0xFF6200EE)
)
// Use LocalContentColor for adaptive colors
Text(
text = "Adaptive color",
color = LocalContentColor.current
)
Font Weights
import androidx.compose.ui.text.font.FontWeight
Text(
text = "Bold text",
fontWeight = FontWeight.Bold
)
Text(
text = "Semi-bold text",
fontWeight = FontWeight.SemiBold
)
Text(
text = "Light text",
fontWeight = FontWeight.Light
)
Font Styles
import androidx.compose.ui.text.font.FontStyle
Text(
text = "Italic text",
fontStyle = FontStyle.Italic
)
Text Decoration
import androidx.compose.ui.text.style.TextDecoration
Text(
text = "Underlined text",
textDecoration = TextDecoration.Underline
)
Text(
text = "Strikethrough text",
textDecoration = TextDecoration.LineThrough
)
Text Alignment
import androidx.compose.ui.text.style.TextAlign
Text(
text = "Center aligned",
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth()
)
Text(
text = "Right aligned",
textAlign = TextAlign.End,
modifier = Modifier.fillMaxWidth()
)
Text(
text = "Justified text with longer content",
textAlign = TextAlign.Justify,
modifier = Modifier.fillMaxWidth()
)
Font Size and Spacing
import androidx.compose.ui.unit.sp
Text(
text = "Custom font size",
fontSize = 24.sp
)
Text(
text = "Custom letter spacing",
letterSpacing = 2.sp
)
Text(
text = "Custom line height\nfor multi-line text",
lineHeight = 24.sp
)
Text Overflow
Ellipsis
import androidx.compose.ui.text.style.TextOverflow
Text(
text = "This is a very long text that will be truncated",
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.width(200.dp)
)
Clip
Text(
text = "This text will be clipped",
maxLines = 2,
overflow = TextOverflow.Clip,
modifier = Modifier.height(40.dp)
)
Visible
Text(
text = "This text remains visible",
overflow = TextOverflow.Visible,
maxLines = 1
)
Multi-line Text
// Soft wrap enabled (default)
Text(
text = "This is a long text that will wrap to multiple lines automatically when it reaches the container width",
softWrap = true
)
// Soft wrap disabled
Text(
text = "This text will not wrap",
softWrap = false
)
// Fixed number of lines
Text(
text = "Multi-line text with maximum 3 lines",
maxLines = 3
)
// Minimum lines
Text(
text = "Short",
minLines = 3 // Reserves space for 3 lines
)
Parameters
modifier
Modifier
default:"Modifier"
Modifier for the text component
color
Color
default:"LocalContentColor.current"
Color of the text
fontSize
TextUnit
default:"TextUnit.Unspecified"
Font size for the text
Font style (Normal or Italic)
fontWeight
FontWeight?
default:"null"
Font weight (Bold, Normal, Light, etc.)
fontFamily
FontFamily?
default:"null"
Font family for the text
letterSpacing
TextUnit
default:"TextUnit.Unspecified"
Spacing between letters
textDecoration
TextDecoration?
default:"null"
Text decoration (Underline, LineThrough, None)
textAlign
TextAlign
default:"TextAlign.Start"
Horizontal alignment of the text
lineHeight
TextUnit
default:"TextUnit.Unspecified"
Line height for multi-line text
overflow
TextOverflow
default:"TextOverflow.Clip"
How to handle text overflow (Clip, Ellipsis, Visible)
Whether text should wrap to multiple lines
maxLines
Int
default:"Int.MAX_VALUE"
Maximum number of lines for the text
Minimum number of lines to reserve space for
onTextLayout
(TextLayoutResult) -> Unit
default:"{}"
Callback for text layout information
style
TextStyle
default:"LocalTextStyle.current"
Base text style to apply
Advanced Usage
Selectable Text
import androidx.compose.foundation.text.selection.SelectionContainer
SelectionContainer {
Text("This text can be selected and copied")
}
Clickable Text
import androidx.compose.foundation.clickable
Text(
text = "Click me",
modifier = Modifier.clickable {
// Handle click
},
color = AppTheme.colors.primary,
textDecoration = TextDecoration.Underline
)
Text with Modifier
Text(
text = "Styled Text",
modifier = Modifier
.fillMaxWidth()
.padding(16.dp)
.background(
color = AppTheme.colors.surface,
shape = RoundedCornerShape(8.dp)
)
.padding(12.dp)
)
Text Layout Information
var textHeight by remember { mutableStateOf(0) }
Text(
text = "Measured text",
onTextLayout = { layoutResult ->
textHeight = layoutResult.size.height
}
)
Text("Text height: $textHeight px")
Best Practices
- Use Typography Styles: Prefer
AppTheme.typography styles for consistency
- Color Inheritance: Use
LocalContentColor for automatic color adaptation
- Accessibility: Ensure sufficient color contrast (WCAG AA: 4.5:1 for normal text)
- Text Overflow: Always handle overflow for dynamic content
- Line Height: Set appropriate line height for readability (1.5x font size is common)
- Max Lines: Use
maxLines with TextOverflow.Ellipsis for truncation
- Performance: Avoid complex text operations in frequently recomposed sections
Accessibility
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
Text(
text = "5",
modifier = Modifier.semantics {
contentDescription = "5 unread messages"
}
)
Source Reference
See the full implementation in Text.kt:31-69 (String) and Text.kt:72-117 (AnnotatedString).