Improve TextSpan docs (#17415)

# Objective

Our
[`TextSpan`](https://docs.rs/bevy/latest/bevy/prelude/struct.TextSpan.html)
docs include a code example that does not actually "work." The code
silently does not render anything, and the `Text*Writer` helpers fail.

This seems to be by design, because we can't use `Text` or `Text2d` from
`bevy_ui` or `bevy_sprite` within docs in `bevy_text`. (Correct me if I
am wrong)

I have seen multiple users confused by these docs.

Also fixes #16794

## Solution

Remove the code example from `TextSpan`, and instead encourage users to
seek docs on `Text` or `Text2d`.

Add examples with nested `TextSpan`s in those areas.
This commit is contained in:
Rob Parrett 2025-02-03 14:36:52 -07:00 committed by GitHub
parent f62775235d
commit adcc80c43d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 3 deletions

View File

@ -158,7 +158,10 @@ impl TextLayout {
}
}
/// A span of UI text in a tree of spans under an entity with [`TextLayout`] and `Text` or `Text2d`.
/// A span of text in a tree of spans.
///
/// `TextSpan` is only valid as a child of an entity with [`TextLayout`], which is provided by `Text`
/// for text in `bevy_ui` or `Text2d` for text in 2d world-space.
///
/// Spans are collected in hierarchy traversal order into a [`ComputedTextBlock`] for layout.
///
@ -173,6 +176,8 @@ impl TextLayout {
/// # let mut world = World::default();
/// #
/// world.spawn((
/// // `Text` or `Text2d` are needed, and will provide default instances
/// // of the following components.
/// TextLayout::default(),
/// TextFont {
/// font: font_handle.clone().into(),
@ -182,6 +187,7 @@ impl TextLayout {
/// TextColor(BLUE.into()),
/// ))
/// .with_child((
/// // Children must be `TextSpan`, not `Text` or `Text2d`.
/// TextSpan::new("Hello!"),
/// TextFont {
/// font: font_handle.into(),

View File

@ -49,7 +49,7 @@ use bevy_window::{PrimaryWindow, Window};
/// # use bevy_color::Color;
/// # use bevy_color::palettes::basic::BLUE;
/// # use bevy_ecs::world::World;
/// # use bevy_text::{Font, JustifyText, Text2d, TextLayout, TextFont, TextColor};
/// # use bevy_text::{Font, JustifyText, Text2d, TextLayout, TextFont, TextColor, TextSpan};
/// #
/// # let font_handle: Handle<Font> = Default::default();
/// # let mut world = World::default();
@ -73,6 +73,12 @@ use bevy_window::{PrimaryWindow, Window};
/// Text2d::new("hello world\nand bevy!"),
/// TextLayout::new_with_justify(JustifyText::Center)
/// ));
///
/// // With spans
/// world.spawn(Text2d::new("hello ")).with_children(|parent| {
/// parent.spawn(TextSpan::new("world"));
/// parent.spawn((TextSpan::new("!"), TextColor(BLUE.into())));
/// });
/// ```
#[derive(Component, Clone, Debug, Default, Deref, DerefMut, Reflect)]
#[reflect(Component, Default, Debug)]

View File

@ -63,7 +63,7 @@ impl Default for TextNodeFlags {
/// # use bevy_color::Color;
/// # use bevy_color::palettes::basic::BLUE;
/// # use bevy_ecs::world::World;
/// # use bevy_text::{Font, JustifyText, TextLayout, TextFont, TextColor};
/// # use bevy_text::{Font, JustifyText, TextLayout, TextFont, TextColor, TextSpan};
/// # use bevy_ui::prelude::Text;
/// #
/// # let font_handle: Handle<Font> = Default::default();
@ -88,6 +88,12 @@ impl Default for TextNodeFlags {
/// Text::new("hello world\nand bevy!"),
/// TextLayout::new_with_justify(JustifyText::Center)
/// ));
///
/// // With spans
/// world.spawn(Text::new("hello ")).with_children(|parent| {
/// parent.spawn(TextSpan::new("world"));
/// parent.spawn((TextSpan::new("!"), TextColor(BLUE.into())));
/// });
/// ```
#[derive(Component, Debug, Default, Clone, Deref, DerefMut, Reflect, PartialEq)]
#[reflect(Component, Default, Debug, PartialEq)]