Topic was automatically imported from the old Question2Answer platform.
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?
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!