Skip to content

Commit 3fec4e0

Browse files
authored
Improve TextSpan documentation (#20484)
# Objective The example in the documentation of `TextSpan` is wrong. It correctly states that `Text` or `Text2d` must be present but won't use either leading to no rendering. It also states that `TextLayout`, `TextFont` and `TextColor` of the first span will all set default values for the children, which is not true. `TextLayout` determines the layout of the block but each node has its own `TextFont` and `TextColor` which are both required components of `TextSpan`. The type description is not clear about `Text` or `Text2d` being mandatory. ## Solution - Fix the documentation
1 parent c7579e7 commit 3fec4e0

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

crates/bevy_text/src/text.rs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -165,41 +165,49 @@ impl TextLayout {
165165

166166
/// A span of text in a tree of spans.
167167
///
168-
/// `TextSpan` is only valid as a child of an entity with [`TextLayout`], which is provided by `Text`
169-
/// for text in `bevy_ui` or `Text2d` for text in 2d world-space.
170-
///
171-
/// Spans are collected in hierarchy traversal order into a [`ComputedTextBlock`] for layout.
168+
/// A `TextSpan` is only valid when it exists as a child of a parent that has either `Text` or
169+
/// `Text2d`. The parent's `Text` / `Text2d` component contains the base text content. Any children
170+
/// with `TextSpan` extend this text by appending their content to the parent's text in sequence to
171+
/// form a [`ComputedTextBlock`]. The parent's [`TextLayout`] determines the layout of the block
172+
/// but each node has its own [`TextFont`] and [`TextColor`].
172173
///
173174
/// ```
174175
/// # use bevy_asset::Handle;
175176
/// # use bevy_color::Color;
176-
/// # use bevy_color::palettes::basic::{RED, BLUE};
177-
/// # use bevy_ecs::world::World;
178-
/// # use bevy_text::{Font, TextLayout, TextFont, TextSpan, TextColor};
177+
/// # use bevy_color::palettes::basic::{BLUE, GREEN, RED};
178+
/// # use bevy_ecs::{children, spawn::SpawnRelated, world::World};
179+
/// # use bevy_text::{Font, Justify, Text2d, TextColor, TextLayout, TextFont, TextSpan};
179180
///
180181
/// # let font_handle: Handle<Font> = Default::default();
181182
/// # let mut world = World::default();
182183
/// #
183184
/// world.spawn((
184-
/// // `Text` or `Text2d` are needed, and will provide default instances
185-
/// // of the following components.
186-
/// TextLayout::default(),
187-
/// TextFont {
188-
/// font: font_handle.clone().into(),
189-
/// font_size: 60.0,
190-
/// ..Default::default()
191-
/// },
185+
/// // `Text` or `Text2d` is needed.
186+
/// Text2d::new("Bevy\n"),
187+
/// // Layout of the entire block of text.
188+
/// TextLayout::new_with_justify(Justify::Center),
189+
/// // TextFont of this node. Won't apply to children.
190+
/// TextFont::from_font_size(50.0),
191+
/// // TextColor of this node. Won't apply to children.
192192
/// TextColor(BLUE.into()),
193-
/// ))
194-
/// .with_child((
195193
/// // Children must be `TextSpan`, not `Text` or `Text2d`.
196-
/// TextSpan::new("Hello!"),
197-
/// TextFont {
198-
/// font: font_handle.into(),
199-
/// font_size: 60.0,
200-
/// ..Default::default()
201-
/// },
202-
/// TextColor(RED.into()),
194+
/// children![
195+
/// (
196+
/// TextSpan::new("Bevy\n"),
197+
/// TextFont::from_font_size(40.0),
198+
/// TextColor(RED.into()),
199+
/// ),
200+
/// (
201+
/// TextSpan::new("Bevy\n"),
202+
/// TextFont::from_font_size(30.0),
203+
/// // Default TextColor will be inserted because TextSpan requires it.
204+
/// ),
205+
/// (
206+
/// TextSpan::new("Bevy"),
207+
/// TextColor(GREEN.into()),
208+
/// // Default TextFont will be inserted because TextSpan requires it.
209+
/// )
210+
/// ],
203211
/// ));
204212
/// ```
205213
#[derive(Component, Debug, Default, Clone, Deref, DerefMut, Reflect)]

0 commit comments

Comments
 (0)