Calculate the required size from the characters in the TextBox

Now, I have a text box in a scroll frame, and I want to calculate the size required for the scroll frame from the number of characters and line breaks in the text box.

For example like this (doesn’t work):

textbox.Changed:Connect(function()
   local size = textbox:GetSizeFromCharacters()
   textbox.Size = size
   scrollframe.Size = size
end)

Are you looking for TextService.GetTextSize?

1 Like

The TextLabel/AbsoluteSize of the text object to be used. Required to compute how the text will wrap.

I also want to add scroll horizontally, Do I need to specify the 4th argument?

Yes, IIRC it’s not optional.
I’d set the X position of the Vector2 to a very high value if how horizontally long the ScrollFrame it is, isn’t a concern Vector2.new(2 ^ 24, 0)

1 Like

If you want to scroll horizontally, why not use X value of AbsoluteSize? But yeh, the forth argument is required.

1 Like

Well, I’m noob at GUI, so can you provide some sample code?

Something like:
print(game:GetService('TextService'):GetTextSize(textbox, 18, Enum.Font.Cartoon, Vector2.new(textbox.AbsoluteSize.X)))
?

It didn’t work… is this wrong?

script:

local textbox = script.Parent.TextBox
local scrollframe = script.Parent

textbox.Changed:Connect(function()
	local size= game:GetService('TextService'):GetTextSize(textbox, 50, Enum.Font.SourceSans, Vector2.new(textbox.AbsoluteSize.X))
	scrollframe.CanvasSize = UDim2.new(0,size.X,0,size.Y)
	textbox.Size = UDim2.new(0,size.X,0,size.Y)
end)

It does work. That’s probably because your TextWrapped is false. If you add a print, and check the size of the textbox from the numbers of characters, you would see it matches.

hmm, still no work…
also, with this change, the characters are no longer displayed

You could just use:

Does it support scrollframe and richtext?

Try reading the post, because I don’t know

I tried it and it was possible, but I don’t see the scrollbar in the scrollframe…

It looks like your defining the text box instance as the first argument when it needs the text of the text box. Adding .Text should fix it

still not working

Onee way to find this out is that you try before asking. I have a plugin wich support horizontal and vertical scrolling, but currently I am on mobile. If I come at my home then I will try to search into the sourceCode how do I made it, because currently I forgot it. I think it was something like this here, but it‘s 99% wrong:

scrollframe:GetPropertyChangedSignal("CanvasSize"):Connect(function()
    scrollframe.CanvasPosition = Vector2.new(scrollframe.CanvasSize.X, CanvasPosition.Y)
end)

But you atleast why your function dosen‘t work, if not I will try to explain it to you:
My theory is that, you know that you can write a 30 long string, but if you have TextScalled to true it won‘t work, because it scales the text so small that it can fit into the textbox. Your string fits into the textbox, so your code is working, just that because it‘s fitting into you don‘t notice the sizechange, because it dosen‘t change. I have the solution, but I am actually on mobile, sorry. I made a plugin, you can try it out say if it‘s that what you are meaning, because I don‘t understand exactly what you want to achieve:

EDIT:
Just saw the video, ok now I understand

Try looking at the textbounds property of the text from. That should help you

2 Likes

It was done using the textbounds property

local textbox = script.Parent.TextBox
local scrollframe = script.Parent

textbox.Changed:Connect(function()
	local size = textbox.TextBounds
	scrollframe.CanvasSize = UDim2.new(0,size.X,0,size.Y)
	textbox.Size = UDim2.new(0,size.X,0,size.Y)
end)

@grif_0, thank you

1 Like

Yeah I had the same issue as you where gettextsize wouldn’t work for me, but it just gave the length in X, and I found the textbounds worked just fine for me.

1 Like