A Label with autowrap on has no width of its own, so inside a container that sizes itself to its content, nothing gives it one. In our Godot 4.7 test a quest line in a PanelContainer came out 1 px wide, 82 lines and 2,129 px tall, about one letter per line, and the same happened next to an icon in an HBoxContainer. A minimum width of 320 px wrapped it into 3 lines, but also made a short Thank you! 320 px wide. Setting the minimum width to the text's own width, capped at 320, gave 3 lines for the long text and an 85 px box for the short one.
The code
Save it as fit_label.gd, load it with const FitLabel := preload("res://fit_label.gd") and call FitLabel.fit(label, 320) after setting the text. Call it again every time the text changes. It turns autowrap on for you.
extends RefCounted
## Makes a word-wrapped Label as wide as its text, up to a limit, so a short
## line stays on one line and a long one wraps at max_width instead of
## collapsing to one word per line inside a container that has no width to give.
## FitLabel.fit($Panel/Label, 320) # call again after changing the text
## Works for tooltips, speech bubbles, dialogue boxes and quest windows.
static func fit(label: Label, max_width: float) -> void:
label.autowrap_mode = TextServer.AUTOWRAP_OFF
label.custom_minimum_size.x = 0
var one_line := label.get_minimum_size().x # the text's own width on one line
label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
label.custom_minimum_size.x = minf(one_line, max_width)
What we measured
A 98-character quest line and a short Thank you!, each in a PanelContainer with a VBoxContainer inside, sized by their content, in the default theme. Two runs gave the same sizes.
- Autowrap off: 747 x 23 px, one line that runs off a small screen.
- Autowrap on: the Label 1 x 2129 px, 82 lines, and the window 1 px wide. Next to a 32 px icon in an HBoxContainer: the same 82 lines, the window 37 px wide.
- Autowrap on and
custom_minimum_size.x = 320: 320 x 75 px, 3 lines. The short text: 320 x 23 px, one line in a box far wider than it. - The script, max 320: the long text 320 x 75 px, 3 lines. The short text 85 x 23 px. Next to the icon: 3 lines, the window 356 px wide.
- Short text changed to the long text without calling it again: 85 px wide, 13 lines. Called again: 320 px, 3 lines.
Why it happens
Without wrapping, a Label's minimum width is its whole line of text. With wrapping, the text can be broken anywhere, so the Label asks for almost no width, and a container that sizes itself to its children gives it exactly that. The Label then wraps at that width, which leaves room for one or two letters per line, and grows taller to fit them. The script measures the text with wrapping off first, then turns wrapping on with that width as the minimum, but never more than your limit.
What to change
- A window that should always be the same width: give the window the width and set the Label's horizontal size flag to Expand Fill instead. In our test a window with a 320 px minimum width gave the same 320 x 75 px and 3 lines.
- Text that types in letter by letter: typewriter text. A window whose corners stretch when it grows: nine-slice windows.
Windows to put the text in: Xianxia UI & Formation Circles has 8 window styles, 24 formation circles, bars, HUD slots and realm badges, every PNG also at @2x, with Godot 4 themes.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Xianxia UI & Formation Circles has 8 window styles, 24 formation circles, bars, HUD slots and realm badges, every PNG also at @2x, with Godot 4 themes.
More how-tos: UI and HUD
- A screen fade that finishes while the game is paused or slowed
- A hand of cards that always fits, without spreading out when it is small
- Edge markers that stay on the rim and point the true way
- Big numbers with thousands separators, minus signs and all
- Show a 0 to 1 ratio on a progress bar, and fill a ring or gauge from the angle you want
- Blink or fade a tinted HUD element without losing its color
- Spin a HUD ring, radar or icon in place instead of around its corner
- Make buttons clickable again under a full-screen fade, transition or post-process ColorRect
- 9-slice window: corners that do not stretch
- Visual novel background: fill the screen, crossfade scenes
- Coins that fly to the HUD counter
- Floating damage numbers in 2D
- An icon over a 3D character's head, without the ghost behind the camera
- Drag a box to select 3D units, in any direction
- Radar blips that turn with the player
- Pick the radial menu slot the mouse is pointing at
- Show a percent that never says 0% or 100% too early
- Type dialogue at the same letters per second on every line and every frame rate
- Place brush dots at the same spacing however fast the mouse moves
- Move a speedometer or gauge needle smoothly without cutting through the bottom
- A countdown that never shows 0 while time is left
- A coin counter that rolls up and lands on the real total