Determining TextBounds for multi-line TextLabels

I’m trying to write a function that will determine the largest font a TextLabel can contain, so that I can maintain the same TextSize for all of my other TextLabels in the frame. Essentially my own version of TextScaling, except it will maintain the same TextSize for ALL TextLabels rather than scaling each individually.

With normal text scaling, my ‘Description’ boxes will have different text sizes:
image

I would like it to look like this:
image

My idea was to use TextBounds to calculate the minimum required TextSize, then apply it to all other TextLabels. My problem is that in order to get multi-line support on the TextLabel I need to enable TextWrapping, but when this is enabled it no longer produces correct TextBounds:

image
image
image

It’s clear from the above image(s) that the Text is overflowing, however the TextBounds thinks that it’s within the limits of the AbsoluteSize.

Does anyone have any suggestions for other ways to calculate this? Or alternative solutions that might produce the same behaviour?

I have just discovered the TextFits property… :upside_down_face:

For anyone interested:

local function FitTextSizeToMaximums(List: GuiObject)
	local TextSize: number = 1; -- Minimum Text Size
	local Maximum: number = 40; -- Maximum Text Size

	local function CheckTextSize(TextSize: number)
		for _,TextLabel in pairs(List:GetChildren()) do
			if (TextLabel:IsA('TextLabel')) then
				TextLabel.TextSize = TextSize;
				if (not TextLabel.TextFits) then
					return false;
				end
			end
		end
		return true;
	end

	repeat
		if (CheckTextSize(TextSize)) then
			TextSize += 1;
		else
			TextSize -= 1;
			break;
		end
	until (TextSize >= Maximum);

	for _,TextLabel in pairs(List:GetChildren()) do
		if (TextLabel:IsA('TextLabel')) then
			TextLabel.TextSize = math.max(Minimum, TextSize);
		end
	end
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.