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

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

Asked by 2 years ago

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?

0
Try using task.wait() to delay it a bit. T3_MasterGamer 2189 — 2y

1 answer

Log in to vote
1
Answered by
enes223 327 Moderation Voter
2 years ago

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))
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? Qwerty_8339 12 — 2y
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 loowa_yawn 383 — 2y
Ad

Answer this question