Resizing text boxes horizontally?

Godot Version

Godot 4.4

Question

I am trying to make a dialogue system with speech bubbles that resize dynamically. They are able to expand vertically with autowrap. However, I also need them to shrink horizontally when only a small amount of text is present.

I tried setting all of the horizontal size flags to “Shrink Begin,” but it didn’t change anything.

Here is what the chat scene looks like. I want the bubbles to shrink down to fit the text:

Here is what my node tree looks like:

Here is the code for the speech bubble scene:

class_name ChatBubble
extends HBoxContainer

@onready var text_label: Label = %text
@onready var speaker_label: Label = %SpeakerLabel
@onready var panel_container: PanelContainer = %PanelContainer

#s
@export var speaker_name: String = "":
	set(value):
		speaker_name = value
		if speaker_label:
			speaker_label.text = value
#s

#Stores values for chat bubble styles
@export_enum("other", "self", "fish", "vitriol", "ocean", "self_expanded") var style: String = "other":
	set(new_val):
		style = new_val
		if Engine.is_editor_hint() or is_node_ready():
			change_style()

@export var text: String = "placeholder message"
@onready var text_box: Label = %text


func _ready() -> void:
	change_style()
	text_label.text = text
	speaker_label.text = speaker_name
	if speaker_name == null:
		style = "self_UI"

#Sets rules for bubble styles and controls how they are called
func change_style() -> void:
	var box = load("res://styles/chat_bubble_%s.tres" % style)
	%PanelContainer.add_theme_stylebox_override("panel", box)
	alignment = BoxContainer.ALIGNMENT_BEGIN
	if style == "self":
		alignment = BoxContainer.ALIGNMENT_END
		speaker_label.visible = false
	if style == "fish":
		alignment = BoxContainer.ALIGNMENT_BEGIN
		speaker_label.visible = true

Your problem is likely in one of the control nodes in the tree you’ve shown. You have a custom_minimum_size property set.

Change the x to something smaller, or to 0 and the problem should go away.

Looks like I did have a custom minimum size set on the text label. I set it to a smaller number, and the text makes a new line for every letter now.

image

Is there a way to tell it to keep going until a certain point?

Not really. You’ll have to create custom code to measure the length of your word and set the minimum to the number of characters times a pixel size per character.