Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to make GUI Size fit the text?

Asked by
blowup999 659 Moderation Voter
6 years ago

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
0
You can edit the CoreGui hiimgoodpack 2009 — 6y
0
What do you mean? blowup999 659 — 6y
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. XAXA 1569 — 6y
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 blowup999 659 — 6y

1 answer

Log in to vote
7
Answered by 6 years ago
Edited 6 years ago

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.

Ad

Answer this question