Text widget displays text content with support for various fonts, sizes, colors, and formatting options.
Basic Usage
Using the text! Macro
Thetext! macro provides format!-like syntax:
Builder Methods
TheText widget is defined in the core library and supports these methods:
size(size: impl Into<Pixels>)
Sets the text size.
color(color: impl Into<Color>)
Sets the text color.
font(font: impl Into<Font>)
Sets the font.
width(width: impl Into<Length>)
Sets the width of the text area.
height(height: impl Into<Length>)
Sets the height of the text area.
align_x(alignment: impl Into<alignment::Horizontal>)
Sets horizontal alignment.
align_y(alignment: impl Into<alignment::Vertical>)
Sets vertical alignment.
line_height(line_height: impl Into<LineHeight>)
Sets the line height for multi-line text.
shaping(shaping: Shaping)
Sets text shaping strategy.
wrapping(wrapping: Wrapping)
Sets how text should wrap.
Color Helper
Use thecolor! macro for convenient color creation:
Rich Text
For more complex text formatting, useRich text with spans:
Text Alignment Example
Styled Text Example
Dynamic Text
Performance Tips
- Avoid recreating static text: Cache text widgets that don’t change
- Use
text!macro: More efficient for formatted strings - Limit font changes: Stick to a few fonts for better performance
- Use appropriate shaping: Use
Shaping::Basicfor simple text
