Positioning of Text2D relative to parent when using TextBounds #18679
Replies: 3 comments
-
Actually, my offset solution doesn't work. If I make one of the words longer than the width of the Add these two additional squares and text to // Longer + long word; needs to wrap; has TextBounds; offset X by BOUNDS_WIDTH / 4.0
// Offset doesn't work
commands
.spawn((
Mesh2d(square.clone()),
MeshMaterial2d(white.clone()),
Transform::from_xyz(-60.0, -180.0, 0.0),
))
.with_children(|parent| {
parent.spawn((
Text2d("Longword with wrap".to_string()),
TextColor(Color::BLACK),
TextFont {
font_size: 15.0,
..default()
},
TextLayout::new(JustifyText::Center, LineBreak::WordBoundary),
TextBounds::new_horizontal(BOUNDS_WIDTH),
Transform::from_xyz(-(BOUNDS_WIDTH / 4.0), 0.0, 0.0),
));
});
// Longer + long word; needs to wrap; has TextBounds; no offset
// Visually slightly to the right of centre
commands
.spawn((
Mesh2d(square.clone()),
MeshMaterial2d(white.clone()),
Transform::from_xyz(60.0, -180.0, 0.0),
))
.with_children(|parent| {
parent.spawn((
Text2d("Longword with wrap".to_string()),
TextColor(Color::BLACK),
TextFont {
font_size: 15.0,
..default()
},
TextLayout::new(JustifyText::Center, LineBreak::WordBoundary),
TextBounds::new_horizontal(BOUNDS_WIDTH),
));
}); |
Beta Was this translation helpful? Give feedback.
-
This feels a bit like #12319, but with added weirdness.
The There may be a specific issue to raise about this ? |
Beta Was this translation helpful? Give feedback.
-
Thanks for the feedback, I've opened #18789 with an example image showing the problem and code to reproduce. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to draw a shape and then write some text on top of the shape. The text should be centred vertically and horizontally on the shape, and should wrap on word breaks.
Looking at the documentation and the examples it seems I need some combination of
TextLayout
andTextBounds
. After much experimentation, including trying to useAnchor
, I discovered that offsetting theTransform
of theText2D
by-(BOUNDS_WIDTH / 4.0)
gave me what I was looking for.My question: is this the best approach? I can't help feeling my solution is a kludge and I'm missing something obvious!
Example code, using Bevy 0.15.3:
Beta Was this translation helpful? Give feedback.
All reactions