Calculate TextScaled size

I need a way to know the TextSize of a string, as it would look if I put it in a TextLabel with TextScaled enabled.

In the dialogue it is seen that the text gets smaller while writing, because it is using TextScaled, but I want it to remain in a single size but fit in the entire TextLabel.

1 Like

UI components that countain text (such as textlabels or buttons) have a readonly property called TextBounds this is a Vector2 calculated in pixels (also known as the Udim2 Offset). using this you can scale e.g. text or a frame to the size of the textlabel’s text offset.

1 Like

I have already tried that, the main problem is that TextBounds is the space that the text occupies, and only when the TextLabel has a text, have I tried putting all the entire text in a TextLabel, and increasing the TextSize until the TextBounds resembles to the AbsoluteSize of the TextLabel, but it’s useless.

Try using this, passing in the frame’s size, and relevant contextual info. This is useful for calculating a “theoretical” TextBox’s TextBounds property.

From there you can calculate the text size by extending the X value of FrameSize infinitely, and reading the Y value of the returned Bounds. By setting the TextSize property to this, it should alleviate your issue.

2 Likes

It always gave 0 to Y, then I realized that I was putting the FontSize as the FontSize of the TextLabel, which varies depending on the TextSize, so I don’t know how to get the FontSize of the scaled size.

I just checked it again and found another readonly property called “TextFits”, and I used a dummy TextLabel and making the TextSize smaller until TextFits is true, then I found the TextSize.

11 Likes

Doing this calculation yourself like that isn’t ideal because it means that every dialog will have a different font size, which results in poor UX. In addition, you’ll still have cases where if a dialog message is too long the resulting text will be unreadably small.

You should pick a font size you know will fit every message you’ll display - either a static size in pixels, or setting the font size based on the height of your dialog bubble using a script. In the future we might add more ways of setting the text size, but until then you have to use scripts to calculate it.

11 Likes

It would be nice if there was an easier way to just read the scaled font size directly. I figured out how to use textbounds on a scaled object but then you have to round up(it seems) to match what the built in scale algorithm does.

I used this on the longest string(to find the smallest scale needed to apply to all similar buttons)

return math.ceil(textObject.TextBounds.Y)

if you wanted this totally automated you’d have to cycle through all the buttons, find the smallest scale then apply it back to all buttons, … not ideal

1 Like