Container won't change its height

Godot Version

4.2.1

Question

Hi, trying to implement following logic:

  • if line can’t fit in 200px I want to enable autowrap and resize container

The issue I have - container won’t change its height when changing size with autowrap label…
A simple example to represent this issue:

@tool extends PanelContainer

func _ready():
	var label: Label = $Label
	label.set_text("Some long text to fill the space and see some results...")
	label.custom_minimum_size.x = 100
	label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
	
	size.x = 200
	size.y = 0 # this line doesn't work as expected

What I want:
Screenshot 2024-01-31 at 11.04.23

I know it may be an expected behaviour, but in my case I just can’t make it work the right way. Even so manually resizing (in the editor) works fine.

I tried using different anchors and content alignments but nothing helped…

You can set a background to the label by setting the Label.normal theme StyleBox

But if you still want to use a PanelContainer then you’ll need to listen to the Label Control.resized signal and change the PanelContainer Control.size to the Label minimum size with Control.get_minimum_size()

Example:

@tool
extends PanelContainer

@onready var label: Label = $Label

func _ready() -> void:
	# set the label minimum width to 200px
	label.custom_minimum_size.x = 200
	# set autowrap
	label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
	# set the size flags to begin to avoid the label being positioned incorrectly when changing the size
	label.size_flags_horizontal = Control.SIZE_SHRINK_BEGIN
	label.size_flags_vertical = Control.SIZE_SHRINK_BEGIN
	# When the label gets resized, change the size of this panel container to the minimum size of the label
	label.resized.connect(func():
		size = label.get_minimum_size()
	)
1 Like

Thank you, it’s working now! Didn’t know how/where to use resized

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