+1 vote

When I create a label from code, and I have it use autowrap, every space in the text gets a line break. Am I doing something wrong? I'm using 3.1.
here's my code, project is at https://github.com/haonkrub/label-autowrap.git

enter image description here

in Engine by (55 points)
edited by

It would seem your label wasn't given a size, so width=0 and words are wrapped to the maximum. However Labels usually determine their size themselves so I'm not sure what could be happening here. Mind sharing a test project?

I added the project to the original post

1 Answer

+1 vote

I had a look at the project, and it's indirectly caused by set_autowrap(true).
When you enable this, you are telling the label to auto-wrap, but in Godot this means it will wrap according to a size you tell it to wrap to (and not the container, like you would have expected), so the label won't give itself a size.

The labels you create have no given size, even though you have set set_h_size_flags(SIZE_EXPAND_FILL), which is only used with some types of containers.

The fix is to use anchors instead:

l.anchor_left = 0
l.anchor_right = 1
by (28,789 points)

Ok that seems to work so long as I also make sure to do l.set_autowrap(true)
Thanks!

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.