Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Resize TextLabel to fit text?

**EDIT:**It appears TextBounds doesn't work at all with plugins. I may file a bug report. In the mean time, anyone else have some ideas to get around this?

I'm creating tooltips which need to expand to fit the size of the given text. I know TextBounds is supposed to give the size of the text, but I'm finding it to be (0,0). This is a part of a plugin, so the TextLabels are a part of the CoreGui (inside a screengui, frame, etc.).

I need to figure out why TextBounds isn't working. Either that or I need to write a function to calculate it( meaning I'll have to figure out the size of each character).

Here's the relevant code

function Tooltip(text, parent)
	local pad = 4
	local tooltip = Create("TextLabel", {
		BackgroundColor3 = Color3.new(1,1,1);
		BorderSizePixel = 0;
		BorderColor3 = Color3.new(1,1,1);
		Text = text;
		ZIndex = 5;
		Parent = parent;
		Create("Frame", { --drop shadow
			BackgroundColor3 = Color3.new(1,1,1);
			BackgroundTransparency = 0.5;
			Size = UDim2.new(1,0,1,0);
			Position = UDim2.new(0,3,0,3);
			ZIndex = 4;
		})
		
	})
	wait()--shouldn't have to, but it was possible TextBounds needed a moment.
	print(tooltip.TextBounds)
	tooltip.Size = UDim2.new(0,tooltip.TextBounds.X + pad,0,tooltip.TextBounds.Y + pad)
	return tooltip
end

--And if you need it the Create function. Likely written by Anaminus since I was borrowing
some of his code which used it.

function Create(ty,data)
	local obj
	if type(ty) == 'string' then
		obj = Instance.new(ty)
	else
		obj = ty
	end
	for k, v in pairs(data) do
		if type(k) == 'number' then
			v.Parent = obj
		else
			obj[k] = v
		end
	end
	return obj
end

Debug info:

 o tooltip.Text is being set - confirmed by a print
 o tooltip is being parented to CoreGui.ScreenGui.ui.panel
 o waiting for tooltip.TextBounds.X to be greater than 0 results in an infinite loop.
 o Setting tooltip.Size does not effect TextBounds

2 comments

0

That's a nice Create function that I may just steal, but that's not where your error is. Do you still bug when you give it some hard-coded text instead of what you passed as an argument?

1

Same results when not using the Create function and giving it pre-set text.

1 answer

a TextLabel has TextBounds so if you use those you should get it.

Example.

TextLabel.Size = UDim2.new(0, TextLabel.TextBounds.X, 0, TextLabel.TextBounds.Y)

3 comments

0

I'm already using TextBounds as I mentioned. The problem is TextBounds property not being set.

0

you cant set a TextBound property it is read only

0

Yes I know, but TextBounds appears to indefinitely be (0,0).

This discussion is preserved as it was. New answers, comments, and votes are closed.