Resizing text box to fit text

Hello everyone,

I am trying to make an inventory tooltip where the items stats and abilities fill out a tooltip panel. My system thus far works reasonably well but it is a bit clunky and I want to learn a better way to do it.

        int textLength = textToAdd.Length;

        currentToolTipSize = new Vector2(currentToolTipSize.x + ((_toolTipText.fontSize * textLength) / _toolTipText.fontSize), currentToolTipSize.y + ((_toolTipText.fontSize * textLength) / _toolTipText.fontSize));

The goal is that every new ability/variable such as durability fits on it’s own line. This works but it often makes the tooltip box to long which is an unfortunate side effect.

Any help would be really appreciated!

There is the best fit option for text size, and the content size fitter for box size.

2 Likes

Best fit isn’t really what I am needing sadly. I need the Tooltip box to change in size as items might have more or less information to display.

Using best fit would give me on occasion text that is just tiny or the text would be massive to fill the space. That or huge amounts of dead space.

Which is why he also mentioned the content size fitter for box size…

The box can adjust horizontally or vertically based on the text you put in it.

View your Canvas, and use the four anchors on your UI elements. It’ll take some adjusting, but it will resize to any screen. I’d recommend editing these on a 16:9 ratio to ensure it resizes correctly.

Content size filter doesn’t work with text boxes, they just ignore it

Works just fine on a text box. I have used it several times.