Why does TextLabel.TextBounds not seem to describe the size needed to fit TextLabel.Text? And other TextLabel woes

So, I’ve created a TextLabel, as shown here:
https://gyazo.com/a9b2308692b9a414951bfabe23ffb574

TextScaled and TextWrapped are off.The TextSize is 14. The size of the TextLabel is (0, 100, 0, 14). The TextBounds are (9, 14). To me, that would suggest that if you have a GUI with size (0, 9, 0, 14), then the text would fit within that container. That sounds reasonable, right?
Yet, as shown by the image, the “g” doesn’t fit within the TextLabel. I put a border of size 1 around the TextLabel to show that it doesn’t fit. Why doesn’t the text fit in the TextLabel if TextBounds.Y is equal to AbsoluteSize.Y?

Additionally, TextFits is true. I can understand this, as TextFits probably just returns true if the TextBounds values are less than or equal to the AbsoluteSize values. But again, my text doesn’t fit. And then the wiki describes TextFits as follows:

The TextFits is a read-only property that is true if rendered TextLabel.Text content does not fit within the GuiBase2d.AbsoluteSize

So TextFits, “text fits,” is true if and only if the text doesn’t fit? What? How does that make any sense? This is wrong, right? TextFits seems to change to false if TextLabel.TextBounds.X > TextLabel.AbsoluteSize.X, but not if TextLabel.TextBounds.Y > TextLabel.AbsoluteSize. Y. What is the reason for this?

If someone could enlighten me I’d really appreciate it, because these two problems have caused me a lot of issues this morning. Thanks.

1 Like

Is this meant for something dynamic that needs to be constantly changed, or is it just something you can tweak once and be done with?

I’m wondering, are you talking about it fitting in the X axis or the Y axis? It’s not really guaranteed that it’ll fit in the Y axis because typeface sizing is really stupid. And by really stupid, I mean… this is an old art, basically. It didn’t translate all that well to computers.

image

As you can see, Roblox considers the height from the baseline to… I’m not really sure, it might be the cap height, might be the ascender height, or it could be something totally arbitrary. However, I do know that Roblox normalizes font sizes such that the TextSize is equal to the Y size that TextBounds and TextService:GetTextSize() will give you.

Other than that, I’m not sure what to tell you, since I doubt anyone has plans to change this API. I always design my UI with 4-8 pixels of padding all around any text, otherwise it just looks bad even if there are no descenders to cut off. It could be that we could request an extra method for getting other metrics like the descender height, but I doubt it’d be a very high priority item.

3 Likes

This is likely caused by the way text is considered sized. The text or font size refers to the pixels (in height) from the base line to the median line of text, however some characters go outside of this bounding (‘g’, ‘y’, ‘p’, ‘q’, ‘j’, ‘,’ for starters) as they go under the base line. A line of text is not measured from the top pixel rendered to the bottom pixel rendered, but instead the base lines used to define the font.

By all technical standards your text does fit, because the bounding boxes of the height of the font and the width of the letters placed together.

1 Like

It’s meant for a chatbox that would resize its y-value based on the amount of text. Based on the other responses, it seems like the best bet is just to add a few extra pixels on the y-axis because of how the text size is defined.

I’m only able to reproduce this with Arial font, I don’t run into this issue when using SourceSans. This might be a pixel rounding issue, I’ll look into it.

The explanation by @TecmagDiams and @mr_smellyman is incorrect. Text is sized according to these metrics:

The idea being that if you set TextSize=14 then a Size=14 frame should be able to fit that text. If that’s not happening then it’s a bug.

8 Likes

I’m so happy to hear Roblox makes sure to adhere to a standard that makes sense! <3 I’m used to working with large font repositories where each font makes it’s own rules.

Thanks for updating (and @'ing) so we can be on the same page.

Thanks. The font shown in my original post was GothamSemibold.

It’d be nice if this was documented somewhere, then. Kinda just have to guess at it.

I have a TextBox, as shown here:
https://gyazo.com/7d049ad5479b26f1d9c332649bf93a9d

The text is size 14, font GothamSemibold. Here is the GUI if you’d like to play around with it yourself:
TextBox.rbxm (2.2 KB)

The text obviously does not fit. Yet, TextBox.TextFits is true. Could this be because of that possible “pixel rounding issue” as well?