+1 vote

I've created a simple text box, which consists of a NinePatchRect that automatically resizes based on its parent Label. I'd like to instantiate this text box multiple times in a VBoxContainer, and have each box grow to match the width of the largest text box in the list.

Current Behavior:
Current behavior

Desired Behavior:
Desired behavior

The only way I have found to do this so far is to attach a script that iterates over all of the labels to find the maximum width, and then set the minimum size of each label based on that value. It feels like there should be a solution to this using just containers and size flags, but I can't figure it out.

I've uploaded a simple example project here.

in Engine by (32 points)

2 Answers

0 votes
Best answer

Clearly I need to pay better attention to the documentation. The issue here is that I'm trying to use a CenterContainer, which the documentation clearly describes as having the following behavior (emphasis mine):

CenterContainer keeps children controls centered. This container keeps all children to their minimum size, in the center.

If I use a MarginContainer instead, the Label resizes as I expected.

by (32 points)
0 votes

I've downloaded your example project, but I have a question. Are you also creating the text for the labels on the fly or do you already know the length of your longest text?

by (134 points)

On the fly. The text for each label comes in from a file, and I don't know ahead of time which labels will be read from the file.

Oh, okay. But you do know the longest line in that file, right? Why do you put it in and see the max size that your NinePatchRect will be having and use it for all other rects?

That's essentially what I am doing in the script that I mentioned.

I don't understand why it's necessary, though. The CenterContainer for each box is resizing as I'd hope, but the Label inside isn't. If I use the built-in Button Controls then they also resize to fill the VBoxContainer.

Is there something wrong with the way that I'm nesting the different Controls in the Scene?

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.