How to make a textlabel the size of the text

image
is there a way to make it so the size of the actual background gui scales with the amount of text?

You can either enable AutomaticSize for the label, or you can use TextService:GetTextBoundsAsync() as AutomaticSize can be a bit finicky.

2 Likes

Yes, you can use the TextBounds property of a TextLabel to determine the size of the text and then adjust the size of the background Frame accordingly:

local gui = script.Parent
local textLabel = gui.TextLabel
local backgroundFrame = gui.Background

textLabel:GetPropertyChangedSignal("TextBounds"):Connect(function()
    local textBoundsSize = textLabel.TextBounds.Size
    backgroundFrame.Size = UDim2.new(0, textBoundsSize.X + 10, 0, textBoundsSize.Y + 10)
end)

This script listens for changes to the TextBounds property of the TextLabel , which is triggered whenever the text size changes. When the text size changes, the script updates the size of the background Frame to fit the text.

In this example, the Background frame has a padding of 10 pixels added to the TextBounds size to give some extra space around the text. You can adjust this value to suit your needs.

2 Likes

Enable TextScaled on the textlabel.

1 Like

I was about to write it here lol.

this solved the problem for me but textbounds property is vector 2 so just Change
textlabel.TextBounds.Size to textlabel.TextBounds and it will work fine