TextService:GetTextBoundsAsync needs to be able to recognize rich text when calculating required container sizes

Problem

As a Roblox developer, it is currently too hard to use rich formatted text when using
TextService:GetTextBoundsAsync() to calculate space requirements of a container that holds text. Currently, the rich text markup is considered as part of the displayed text when calculating the needed space. This results in too much space being allocated for text. See the below visuals for illustration of the issue.

Normal

Rich Text

Video
robloxapp-20240401-1336153.wmv (161.3 KB)

One can load the below file into Studio so they can see the issue.
Engine - TextService-GetTextBoundsAsync.rbxl (58.9 KB)

Currently, the only ways around this is to

  1. Have two text strings. One contains rich text that is displayed, the other without the rich text which is used for space calculations.
  2. Have one text string, but use a function/method to produce a copy of the string with all rich text markup removed to be used in space calculations.

Conclusion

If Roblox is able to address this issue, it would improve my development experience because then rich text can be used as-is when calculating space requirements when using
TextService:GetTextBoundsAsync() without the need for elaborate workarounds.

9 Likes

There is a simple work around to this. Applying the text to a TextLabel, you can then read the ContentText, which itself is the text with no rich text markups

local text = textArray[(index % #textArray) + 1]
textLabel.Text = text
tbParams.Text = textLabel.ContentText
1 Like

Interesting. What I did to get around it is to make a second string from the first one and strip all the Rich Text tags from the new string. Then I run the that string through it to get the size.

1 Like