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

Resizing the TextLabel according to it's contents?

Asked by 7 years ago

I have a chat GUI that, when someone chat's, a TextLabel is created in which will contain what the player said. Though, i would like that when each line that the text creates, it resizes the Y of the text box accordingly, so it doesn't have a static Y size and when the player chats a short message, there isn't a giant gap between each message.

Script:

wait(2)

chatBoxGui = script.Parent.ExampleGui
chatEvent = game.Workspace:WaitForChild("OnChatted")
receiveChat = game.Workspace:WaitForChild("SendChatToEveryone")
amountOfChats = 0
canChat = true

function CreateNewChat(args)
    local newChat = chatBoxGui:clone()
    newChat.Parent = script.Parent.Parent.Chats
    newChat.Position = UDim2.new(0,0,0,amountOfChats*50)
    newChat.Text = args[1]..": "..args[2]
    newChat.Visible = true
    amountOfChats = amountOfChats+1
end

receiveChat.OnClientEvent:connect(function (args)
    CreateNewChat(args)
end)

script.Parent.Parent.TextButton.MouseButton1Click:connect(function()
    if not canChat then return end
    canChat = false
    chatEvent:FireServer({game.Players.LocalPlayer.Name,script.Parent.Text})
    wait(2)
    canChat = true
end)

1 answer

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

You can enable the TextWrapped property of TextLabel (so the text will align itself accordingly to it's text bounds), then use the TextBounds property to get the height of the message. You can index TextBounds for the axis you want the length of (either X or Y), in this case, you'd use Y. After that, simply set the size of the TextLabel with TextBounds.Y as your Y offset. Here's an example:

newChat.Text = "random multi-line\ntext"
newChat.Size = UDim2.new(1,0,0,chatMessage.TextBounds.Y)
0
Thanks! DragonOfWar900 397 — 7y
Ad

Answer this question