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

How to make GUI Size fit the text?

I have a GUI that will contain usernames that I want to fit exactly around the username.

I was wondering if there was any way to do this other than

while not button.TextFits do 
	button.Size = button.Size + UDim2.new(0, 1, 0, 0)
end

4 comments

0

You can edit the CoreGui

0

What do you mean?

1

There isn't an easy way to "shrinkwrap" the size of the gui to fit the text in it. You can try a script that uses http://wiki.roblox.com/index.php?title=API:Class/TextService/GetTextSize to get the size of the text ahead of time, though this will mean that you will have to resize the gui yourself via script.

1

I believe I found a solution. http://wiki.roblox.com/index.php?title=API:Class/TextLabel/TextBounds TextBounds property is supposed to have a Vector2 for the size of the text

1 answer

✓ Accepted answer

You can use the TextService which contains the function GetTextSize that allows you to calculate the size of a frame needed for the inputted test along with its font and size.

A small example:-

local textServ = game:GetService('TextService')
local button = [some path to the button]
local newSize = textServ:GetTextSize(button.Text, button.TextSize, button.Font, button.AbsoluteSize) -- passes back a Vector2

-- set new button size
button.Size = Udim2.new(0, newSize.X, 0, newSize.Y)

I hope this helps.

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