--- name: bevy-text description: Reference for text rendering in Bevy — Text vs Text2d, styling, text spans, changing text at runtime, clicking text, and font rendering. metadata: crate: bevy_text bevy: "0.19" --- ## Text vs Text2d | Component | Use case | |-----------|----------| | `Text` | UI text (fixed position, in HUD/inventory) | | `Text2d` | Scene text (floating damage, world-space labels) | ## Creating text ```rust // In scene commands.spawn(( Text2d::new("Hello"), TextFont::from(font_handle).with_font_size(FontSize::Px(60.0)), TextColor::WHITE, // `TextLayout::justify` only aligns the lines *within* the block of text. TextLayout::justify(Justify::Center), // Wrapping and truncation are controlled by `TextBounds`, not by the `Node`. TextBounds::new_horizontal(300.0), // `Anchor` controls where the text sits relative to its `Transform`. Anchor::TOP_LEFT, Text2dShadow::default(), )); // In UI commands.spawn(( Node { position_type: PositionType::Absolute, bottom: px(5.0), right: px(5.0), ..default() }, Text::new("Hello"), TextFont::from(font_handle), )); ``` ## Styling components - `TextLayout` — alignment, line break behavior - `TextFont` — font, font size (`with_font_size(FontSize::Px(..))`) - `TextColor` — color override - `TextBounds` — wrapping/truncation (not the `Node`) - `Anchor` — where text sits relative to its `Transform` (`Text2d`) - `Text2dShadow` — shadow rendering for `Text2d` ## Multi-style text (TextSpan) ```rust commands.spawn(( Text::new("Here is some text. But "), TextFont::from(regular_font), children![( TextSpan::new("this part is bold"), TextFont::from(bold_font), )], )); ``` ## Changing text at runtime ```rust fn update(mut texts: Query<&mut Text, With>) { for mut text in &mut texts { text.0 = "New text".to_string(); } } ``` ## Clicking text ```rust fn handle_click(event: On>, mut query: Query<&mut Text2d>) { query.get_mut(event.entity).unwrap().0 = "Clicked!".to_string(); } commands.spawn(Text2d::new("Click me")).observe(handle_click); ``` ## Font files Supported: `ttf`, `otf`. Default font: Fira Mono. Load via `AssetServer`. Bevy 0.19+ supports OpenType font features.