Godot how to center text on label?

Viewed 10284

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" ]
2 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.

Just set the Align and Valign properties to Center to center the text. The bounding rect of the label has to be scaled to actually see the effect. You can do that by dragging the control points of the rect in the 2D view or change the "Margin" or "Size" of the rect in the "Control" section of the inspector.

Related