How do I center a Label inside a Node in Godot?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Adrián Arroyo Calle

'm trying to center a label inside a custom drawing node in Godot. The label is attached to the node. The parent node is an hexagon, which (0,0) position is in the center of the hexagon (that means, there are negative coordinates in the node). When I add the label it seems to take the center of the hexagon as its top-left corner.

However, I would like to have the center of the label in the center of the hexagon. That means, some parts of the label must have negative coordinates, but I can’t find any solution to center the label the right way. Labels don’t have size on Godot so I can’t center it manually.

Code of Label:

var label = new Label();
tile.AddChild(label);
label.SetText("1");
label.SetScale(new Vector2(1.5f,1.5f));
label.SetAlign(Godot.Label.AlignEnum.Center);
:bust_in_silhouette: Reply From: Fish_in_a_barrel

I centred the text, adjusted the rect of the label to be symmetrical and then offset the label by that amount.

var margin_size = 30
label.align = 1
label.valign = 1
label.margin_left = -margin_size
label.margin_top = -margin_size
label.margin_right = margin_size
label.margin_bottom = margin_size
label.rect_position = Vector2(-margin_size,-margin_size)

You could have different sizes for left/right compared to up/down.

*Edit: I see now you are using c#… this is for GDscript, I assume there are similar properties available.