+1 vote

So basically the player is being asked a question with multiple options. Some options may be one word, some may be many. So i need to set a maximum width to the label but have a variable height according to the length of text.

in Engine by (292 points)

3 Answers

0 votes

I tried to fiddle with Label but I can't find an option to resize it according to the text inside. If you know how many lines an option will have, you may be able to create labels dynamically in a script by offsetting them by label.get_line_length() * line_count, but if word wrapping is enabled I don't see an option to get the number of wrapped lines. So you end up with a compromise with no word wrapping and manual line count for each option.

If you think the feature is missing you can eventually ask for it on Github, however even with it you would still have to do some layout scripting to get what you want.

I tried an alternative: putting labels in a VBoxContainer. It layouts them automatically fine, however word wrapping simply doesn't work, it shrinks the label to an unreadable size :/

Edit: I just opened an issue about word wrapping behaviour https://github.com/godotengine/godot/issues/7679

by (28,789 points)
edited by
+7 votes

Well. It's been a long time since you posted this but I just had the same problem and nobody seems to have solved it. Hope this helps.This is what I did and actually did the trick perfectly:

$Label.rect_size = $Label.get_font("font").get_string_size($Label.text)

I do this in a custom control node I made that uses a touchscreenbutton that resizes along with the label and the control node it self anytime I change the text via script.

What this does is getting the string size (returns a vector2) based on a string using the font you got before using the "getfont()" method as character size refference. You can pass "font" as argument to the getfont() method to pass your current label font.

by (32 points)

Thank you! That's exactly what I was looking for :)

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 (40 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 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 webmaster@godotengine.org with your username.