How to auto-scale a text label by using the string size?

Hi, I’m wondering on how I can auto-sale a text label by the string size that is stored in the text label. I’ve been trying and trying but I couldn’t figure out on how to do so. I want the text label to auto-scale on the Y-Axis, but I can’t figure it out. I used string.len to figure out the length of the string then figure out if it’s greater than my maximum string length but I want the Gui to auto sale to the string length. If anyone can help that’ll be great!

8 Likes

Is the TextScaled property what you’re looking for?

No, I used that right now but I want the Text Label size to increase so the text doesn’t get really small, it get’s really small and it looks horrible. Thanks for the reply!

1 Like

I’ll give you an example of what happens.

Testing

The TextBounds property returns a Vector2 value referring to the size of the text displayed. Setting the TextLabel’s size to a value greater than or equal to the X and Y values will always ensure that the text fits inside the TextLabel.

18 Likes

This is exactly what I was looking for, thanks so much man!

1 Like

In addition to what JoshRBX said, you can use UITextSizeConstraint (together with the TextScaled property) to set a minimum and maximum TextSize value.

4 Likes

You guys are amazing thanks for the help!

1 Like

It is important to note that TextBounds can only be used if you have an existing TextLabel.
If you’re creating them in a script, and want to have it already sized before you have it show up, you should use TextService.

You’ll be using the :GetTextSize() function. Read up on it here.

This function is a useful alternative to the TextLabel.TextBounds property of the TextLabel and TextButton objects. Using the TextLabel.TextBounds property to calculate the dimensions text requires is often impractical as it requires a TextLabel object to be created.

With GetTextSize, the dimensions required by a particular text string in a particular TextLabel or TextButton can be calculated before any object is created or text property set.

I hope you find this helpful!

23 Likes

This will be going live very soon and would be your best option:

12 Likes

Hi, I used @boatbomber suggestion! It worked really well, I’ll be using the new feature once it comes out since it makes it so much easier!

Thanks so much for the reply!

1 Like