Calculate the best TextSize for a frame

I’m working with RichText, so I can’t use TextScaled to make sure my text fits inside the frame.

For this frame, the best TextSize is 17.
image

But when I resize the frame, I want to resize the text to fit perfectly in the frame.
image (17 is just a tad bit small for this one…)

How do I calculate this? Help is appreciated.

There are 2 options that came to my mind. You can easily use the TextScaled option or you just do this loop to check for every value if it fits. I’m not sure if the second one makes sense but anyways.

local TextLabel = 'Your Path'

for i = 1, 100 do
	if not TextLabel.TextFits then
		TextLabel.TextSize = i-1
		break
	end
	TextLabel.TextSize = i
end
1 Like

This worked. Thank you.

local function ScaleText(TL)
	if TL:IsA("TextLabel") or TL:IsA("TextButton") or TL:IsA("TextBox") then
		
		local BestSize = 1
		for i = 1,100 do
			TL.TextSize = i
			if not TL.TextFits then
				TL.TextSize -= 1
				break
			end
		end
	end
end