diff --git a/crates/bevy_text/src/text.rs b/crates/bevy_text/src/text.rs index 6c63690867..f564a3078a 100644 --- a/crates/bevy_text/src/text.rs +++ b/crates/bevy_text/src/text.rs @@ -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(), diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index 051ed5c6f3..952627a6b7 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -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 = 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)] diff --git a/crates/bevy_ui/src/widget/text.rs b/crates/bevy_ui/src/widget/text.rs index f8738c9c38..9dec471037 100644 --- a/crates/bevy_ui/src/widget/text.rs +++ b/crates/bevy_ui/src/widget/text.rs @@ -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 = 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)]