VERTICAL_ALIGNMENT_CENTER won't center?

Godot 4.3

The image shows on the right, a label node vs one created in gd script on the left


None of the ones in gd script are vertically centering.

Anyone know how to make the text center vertically?

func create_label(new_text: String, new_position: Vector2, new_size: Vector2, new_fontsize: float, new_alignment: int) -> Label:
	var label := Label.new()
	label.text = new_text
	label.position = new_position - new_size * 0.5  # Adjust position to center
	label.size = new_size
	label.horizontal_alignment = new_alignment
	label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
	label.size_flags_vertical = Control.SIZE_EXPAND_FILL  # Expand and fill vertically

	# Create and configure LabelSettings
	var label_settings := LabelSettings.new()
	label_settings.font = ThemeDB.fallback_font
	label_settings.font_size = new_fontsize
	label.label_settings = label_settings

	var debug_rect := ColorRect.new()
	debug_rect.size = new_size
	debug_rect.color = Color(1, 0, 0, 0.3)  # Semi-transparent red
	label.add_child(debug_rect)

	return label

Couldn’t find info on this and chat gpt couldnt figure it out either :sweat_smile:, please help

There’s alignment inside the label, which it looks like you’ve done, and there’s alignment of the label itself. What happens if you subtract 2 from the labels’ position.y?

label.position -= Vector2(10.0,10.0);


Exactly what you would expect.

Awesome. One step closer. What happens if you make the font size smaller?

1 Like

label_settings.font_size = new_fontsize - 8

Then it centers.

The font size I used was smaller then the size of the rect though.
The title font size was 32 and the size 34 pixels.
The other areas font size was 18 and the size was 28 pixels

I did this as a temp fix, but how can I know what size the rect needs to be for the label to start working? or do I need to anchor it to center because it is forcing the label to expand downwards. from top left anchoring. I’ll try more. Lmk if anyone else has something. thanks


	label.custom_minimum_size = Vector2(new_size.x,max(new_size.y, new_fontsize * 1.5)) # Force correct size
	label.size = label.custom_minimum_size;

Well I found the solution, If I set the custom_minimum_size and set size to it, godot will automatically set it to the min size for vertical alignment to work.

label.custom_minimum_size = Vector2(new_size.x, 0)
label.size = label.custom_minimum_size

Just need to make sure the label settings are applied first.

This is the updated code, ignore the added anchoring stuff, that was just to simplify some of the stuff I was doing.

func create_label(new_text: String, new_position: Vector2, new_size: Vector2, new_fontsize: float, new_alignment: int) -> Label:
	var label := Label.new()
	label.text = new_text

	# Apply font settings
	var label_settings := LabelSettings.new()
	label_settings.font = ThemeDB.fallback_font
	label_settings.font_size = new_fontsize
	label.label_settings = label_settings

	# Set correct size
	label.custom_minimum_size = Vector2(new_size.x, 0)
	label.size = label.custom_minimum_size

	# Set Anchoring (Centering) IGNORE THIS FOR SOLUTION
	label.set_anchors_preset(Control.PRESET_CENTER)
	label.pivot_offset = label.size * 0.5  # Center pivot to match Godot Editor behavior

	# Adjust position to respect anchoring  INGNORE THIS FOR SOLUTION
	label.position = new_position - label.size * 0.5

	# Set alignment
	label.horizontal_alignment = new_alignment
	label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER

	# Debugging: Add a ColorRect to visualize label bounds
	var debug_rect := ColorRect.new()
	debug_rect.size = label.size
	debug_rect.color = Color(1, 0, 0, 0.3)  # Semi-transparent red
	label.add_child(debug_rect)

	return label

Now I can just set

label.custom_minimum_size = new_size

and it should just resize it if its too small automatically.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.