Right now, it is very difficult to get the bounds of a string with rich text components. I am attempting to upgrade my chat GUI to not use a custom rich text module in favor of Roblox’s built-in renderer, and part of the system relied on using TextService to gauge how big a chat’s container frame needed to be.
Due to the TextService not providing an option to handle rich text, it is nearly impossible to get an appropriate size. While manually cutting out XML tags does partially work, this causes problems for things like bold text, which has a different scale than its non-bold counterpart.
The current workaround I use is to create a dummy TextLabel that mimics a TextLabel in my chat GUI, set its text and parent it to the player’s GUI in an off-screen location, and grab the TextBounds property. This is, for obvious reasons, less than ideal.
Adding onto this, a parameter for LineHeight would be appreciated as well. Wanting to add some spacing between lines doesn’t seem to be accounted for with the current method.
I’ll bump again since this feature is much needed.
As far as I can see it could be as simple as extending an additional property onto GetTextBoundsParams for the GetTextBoundsAsync(params) TextService method. For instance ObeyRichText which defaults to false:
local params = Instance.new("GetTextBoundsParams")
params.Text = "<b>Shop</b>"
params.Font = Font.new("rbxasset://fonts/families/GrenzeGotisch.json", Enum.FontWeight.Thin)
params.ObeyRichText = true
local result = TextService:GetTextBoundsAsync(params)
For anyone still facing this problem here is probably the best workaround for color related tags only:
local TextService = game:GetService('TextService')
local function getTextSize(text: string, fontSize: number, font: Enum.Font, frameSize: Vector2?): Vector2
if text:find('<font') then
text = text:gsub('<[^>]+>', '')
end
return TextService:GetTextSize(text, fontSize, font, frameSize or Vector2.new(math.huge, math.huge))
end
This simple function wrapper will get rid off every rich text component before actually calculating its size, here is example:
Before:
Some <font color="#e66464">test</font> string
After:
Some test string
As a result TextService:GetTextSize is again 100% accurate.
This is great until you use inline rich-text font sizing. Might be easier to just have a TextLabel dedicated to this and using the TextBounds property.