How to change canvas size based on text length?

Hi I am trying to change the canvas size based on the length of the text so that the text will fit in the textbox.

I have tried using TextBox | Roblox Creator Documentation and TextService | Roblox Creator Documentation but it doesn’t work for me.

Have you tried using the TextWrapped/TextScaled properties to scale the text according to the size of its bounding box?

I am using TextWrapped but not TextScaled because I want to set the text size.

In that case, enable the AutomaticSize property of the TextBox/TextLabel instance.

Set the TextBox/TextLabel to dynamically size along the x-axis in accordance with the real-estate (space) taken up by the contained text if the text goes from left-to-right (or right-to-left), y-axis if the text goes from top-to-bottom (or bottom-to-top) and finally you can make the containing TextBox/TextLabel dynamically resize itself along both axis (x and y) if it should contain/hold text which spans along both axis (such as a paragraph).

Would it work if the size of the textbox is set to {1, 0},{1, 0}?

Yes, since AutomaticSize overrides whatever UDim2 value is used to describe the “Size” property of the GuiObject.

I have Set ScrollingFrame.AutomaticCanvasSize to Y and TextBox.AutomaticSize to Y and sadly it didn’t work.

Here’s an example of AutomaticSize dynamically resizing a GuiObject along the y-axis (it does work).

https://gyazo.com/361942ac660284970362ab1ba4ccbe4e

As you can see, when the TextBox/TextLabel instance is required to hold/contain more text its size is dynamically increased.

Bare in mind that AutomaticSize will not increase the size of a GuiObject such that it overlaps the edges of its ancestor GuiObject instances.

TextWrapped is required (otherwise the text will end up potentially extending outside of the bounds of the TextBox/TextLabel instance), that was the only property I enabled other than AutomaticSize itself.

AutomaticSize works but the size doesn’t increase more than {1, 0},{1, 0}

So that doesn’t do what I want to achieve.

If you’re attempting to dynamically resize a GuiObject according to its contained text without using AutomaticSize then you’re going about it the wrong way.

You can check the TextFits property. If false, increase the canvas size by x amount.

while not TextBox.TextFits do
	TextBox.Size += UDim2.new(0,0,0,textYSize)
end

Hi, that works but sometimes it increases the canvas size property even if the text fits and I don’t want that to happen.