Skip to content

Positioning of text with line breaks is offset #717

@tigregalis

Description

@tigregalis

Using bevy master, latest commit c743a96, on Windows 10.

Possibly related to #81, #401 and #182 (#183).

I'm trying to position text ("this\ntext\nwraps") that has line breaks in it in the top right corner of the window like so:

____________________________
                     this  |
                     text  |
                     wraps |

I use

            style: Style {
                position_type: PositionType::Absolute,
                position: Rect {
                    top: Val::Px(5.0),
                    right: Val::Px(5.0),
                    ..Default::default()
                },
                ..Default::default()
            },

It appears to calculate the width of the rectangle from the text without the line breaks ("thistextwraps").
Then when it is trying to position the text in the top right, it takes this rectangle (without the line breaks), then afterwards it wraps the text.

Minimal example:

use bevy::prelude::*;

fn main() {
    App::build()
        .add_default_plugins()
        .add_resource(ClearColor(Color::BLACK)) // the window's background colour
        .add_startup_system(setup.system())
        .run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands
        .spawn(UiCameraComponents::default())
        .spawn(TextComponents {
            text: Text {
                font: asset_server.load("fonts/FiraSans-Bold.ttf"),
                value: "This\ntext\nwraps".to_string(), // this is the text I want to position in the top right
                style: TextStyle {
                    color: Color::rgba(1.0, 1.0, 1.0, 0.5), // White text
                    font_size: 40.0,
                },
            },
            style: Style {
                position_type: PositionType::Absolute,
                position: Rect {
                    top: Val::Px(5.0),
                    right: Val::Px(5.0),
                    ..Default::default()
                },
                ..Default::default()
            },
            ..Default::default()
        })
        .spawn(TextComponents {
            text: Text {
                font: asset_server.load("fonts/FiraSans-Bold.ttf"),
                value: "Thistextwraps".to_string(), // this is a "shadow" that demonstrates what's happening
                style: TextStyle {
                    color: Color::rgba(0.7, 0.7, 1.0, 0.5), // Blue text
                    font_size: 40.0,
                },
            },
            style: Style {
                position_type: PositionType::Absolute,
                position: Rect {
                    top: Val::Px(5.0),
                    right: Val::Px(5.0),
                    ..Default::default()
                },
                ..Default::default()
            },
            ..Default::default()
        });
}

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-UIGraphical user interfaces, styles, layouts, and widgetsC-BugAn unexpected or incorrect behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions