Text2d

Text2d

  1. String

Description

The top-level 2D text component.

Adding Text2d to an entity will pull in required components for setting up 2d text. Example usage.

The string in this component is the first 'text span' in a hierarchy of text spans that are collected into a [ComputedTextBlock]. See TextSpan for the component used by children of entities with [Text2d].

With Text2d the justify field of [TextLayout] only affects the internal alignment of a block of text and not its relative position, which is controlled by the [Anchor] component. This means that for a block of text consisting of only one line that doesn't wrap, the justify field will have no effect.

# use bevy_asset::Handle;
# use bevy_color::Color;
# use bevy_color::palettes::basic::BLUE;
# use bevy_ecs::world::World;
# use bevy_text::{Font, JustifyText, Text2d, TextLayout, TextFont, TextColor};
#
# let font_handle: Handle<Font> = Default::default();
# let mut world = World::default();
#
// Basic usage.
world.spawn(Text2d::new("hello world!"));

// With non-default style.
world.spawn((
    Text2d::new("hello world!"),
    TextFont {
        font: font_handle.clone().into(),
        font_size: 60.0,
        ..Default::default()
    },
    TextColor(BLUE.into()),
));

// With text justification.
world.spawn((
    Text2d::new("hello world\nand bevy!"),
    TextLayout::new_with_justify(JustifyText::Center)
));

Functions