+1 vote

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

in Engine by (310 points)
edited by

2 Answers

+1 vote
Best answer

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 ....")
by (9,796 points)
selected by

This works beautifully, thank you.

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 settext. the itemrect_changed signals don't work if autowrap is enabled and found some other issues to

0 votes

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 :)

by (38 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.