Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Godot how to center text on label?

I just made a new Godot project and created a Label with text. Even with Align: center the text stays top left when I run the game.

Label.tscn

[gd_scene format=2]

[node name="Label" type="Label" index="0"]

anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_right = 40.0
margin_bottom = 14.0
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
mouse_filter = 2
mouse_default_cursor_shape = 0
size_flags_horizontal = 1
size_flags_vertical = 4
text = "heylab"
align = 1
valign = 1
percent_visible = 1.0
lines_skipped = 0
max_lines_visible = -1
_sections_unfolded = [ "Anchor" ]
like image 976
shwick Avatar asked Sep 09 '18 16:09

shwick


People also ask

How do you center an object in Godot?

For Control nodes, you can set their anchors to be centered (choose Layout -> Center) in the menu, or make them a child of a CenterContainer (if you need a dynamic layout for different sized screens).

How do I center text in a container in bootstrap?

Add class . text-center to the parent div to align text or any item inside to the center. You can also decide how the alignment should look like on the specific screen sizes.

How do I move labels in Godot?

1 Answer. There are two ways: Add a CanvasLayer to the player then move the Label under it. Move the Label outside of the player.


1 Answers

After setting Align and Valign to Center, you have to adjust both anchors and margins. You have some different options to do that.

  • Choosing "Center" in Layout will center your label by adjusting anchors to 0.5 (center of screen), and will calculate Margins, so that the Rect will be centered (without changing its size).

  • Choosing "Full Rect" in Layout will set anchor to (0, 0, 1, 1, that is the full screen), margins to 0, and will change the Rect of your Label node, so that the node will fill the screen.

The Layout button appears in the toolbar when you select Control nodes (Labels, Containers etc). screenshot to show Layout button in Godot 3

Obs.: It's probably better to first create a Container node, set Container node to Full Rect, then create child nodes for your label. Your anchors are set inside the parent's Rect.

like image 158
Gustavo Kaneto Avatar answered Sep 26 '22 08:09

Gustavo Kaneto