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

Is there a way to make the size of a TextLabel adjust to how many lines of text there are?

So I'm making a gui where things like errors, warnings, and other general info print out onto a gui so it's easier for myself to read.

However I ran into an issue, where if something copies over that spans multiple lines, it will overlap with the other lines that were made previously

This is some of the code I have for the gui right now.

local canvasX = 0
game:GetService("LogService").MessageOut:connect(function(output, messageType)
	local newLine = script.NewLine:clone()
	newLine.Position = UDim2.new(0, 5, 0, (#script.Parent.Scrolling:getChildren() * 15))
	newLine.Text = "> " ..output
	newLine.Name = tick()
	newLine.Parent = script.Parent.Scrolling
	newLine.Size = UDim2.new(0, (#output * 10), 0, 15)
	canvasX = (canvasX > (#output * 10) and canvasX or (#output * 10))
	script.Parent.Scrolling.CanvasSize = UDim2.new(0, (canvasX + 10), 0, ((#script.Parent.Scrolling:getChildren() + 1) * 15))
end)

And this is what it looks like printing single lines, compared to when it prints a message with multiple lines.

Single Line: https://i.imgur.com/BJsLxh1.png

Multiple Lines: https://i.imgur.com/gFOJVcI.png

As you can see, my script will copy singular lines just fine, but when something spans multiple lines it will cause interference.

Is there any way to make the TextLabel adjust to how many lines the text spans?

1 comment

0

Try using task.wait() to delay it a bit.

1 answer

✓ Accepted answer

If your TextLabel's text is wrappable and is not automatically sized you can just use the TextBounds property, it shows how much space does the current text take. Example:

local textSize = label.TextBounds
local defaultSize = label.Size
label.Size = UDim2.new(0, defaultSize.X.Offset, 0,  math.max(textSize.Y, defaultSize.Y.Offset))

2 comments

0

This worked! However, I'm having trouble with having new line go to the bottom of the one with multiple lines. How would I be able to connect a new line to the bottom of the one that is multiple lines long?

0

You would have to modify the way your code handles positioning of the TextLabel by looping through all the objects and adding their Y offset together

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