Label autowrap dynamic height

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By supaiku
:warning: Old Version Published before Godot 3 was released.

Hi,

I’m can’t figure out how to dynamically size a label using autowrap. I made an example project where I have several labels in a VBoxContainer. What I want to achieve is that the labels wrap and grow in height, but instead they are always cut off.

The same issue could be applied to RichTextLabels, where I found some hacks using yield(get_tree(), "idle_frame") and then setting the height to the vscrollbars get_max(), but the resulting height is always too big. But let’s focus on Labels for now.

project

:bust_in_silhouette: Reply From: volzhs

First of all, you need some space character in string to use autowrap.

SuperLabel.gd

extends Label

func _ready():
	connect("item_rect_changed", self, "on_item_rect_changed")

func on_item_rect_changed():
	set_custom_minimum_size(Vector2(0, (get_line_height()+get_constant("line_spacing")) * get_line_count()))

Root.gd

LabelContainer.get_children()[1].set_text("AABCAB CABCA BCABC ABCABC ....")

This works beautifully, thank you.

supaiku | 2016-10-13 13:28

ty this works. however if anyone sees this in 2.1.4++, just do:

 yield(get_tree(), "idle_frame")
updateText() # same code inside on_item_rect_changed

and call this after set_text. the item_rect_changed signals don’t work if autowrap is enabled and found some other issues to

wombatTurkey | 2017-09-19 01:58

:bust_in_silhouette: Reply From: annemarietannengrund

it may be a bit late, but since this topic was found when i searched my issue.

these codesnippets helped my implement a solution for big or unknown length label content that i want wrapped inside a container.

https://github.com/annemarietannengrund/theGodotExperience/tree/main/Examples/Example1

i tried to give some example layouts, pointing out the key elements for the implementation and have it ready as a demoscene to try it out.

hf&gl :slight_smile: