Label autowrap makes line breaks at every space

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Noah Burck

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 GitHub - henlo-birb/label-autowrap

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?

Zylann | 2019-03-29 13:39

I added the project to the original post

Noah Burck | 2019-03-29 16:06

:bust_in_silhouette: Reply From: Zylann

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

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

Noah Burck | 2019-03-30 00:51