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

How do I use TextLabel.TextBounds?

I have a few lines of code that are supposed to change the text label's Y offset if the text doesn't fit, and the only way I can get the text to loop to a new line is with TextWrapped, which also wraps the top and bottom. Here is a sample:

if cht.TextFits == false then
	cht.TextWrapped = true
	repeat cht.Size = cht.Size + UDim2.new(0,0,0,15) until cht.TextFits == true
end

Sadly I don't have any other examples, and with that example code, the size stays at it's original size of 1,0,0,15 instead of adding length to Y because the TextWrapped also wraps the bottom and top of the label.

Until ROBLOX has enough sense to add a text wrapping that only wraps the sides, I need to figure out how to use TextBounds.

Is there any way to set a limit on the TextBounds.X and then have Y expand accordingly? Or else, how am I supposed to do this?

Thanks in advance for any replies I might get, I've been working on a large chat-based project for the past week and have had this error be produced with no fix for the past 2 days of my used 7, and I only have another week before my "deadline".

3 comments

0

Why not use TextScaled?

0

Why not just make the text box smaller?

0

Using TextScaled would make the text smaller, which I don't want, and making the text box smaller would be counterproductive.

1 answer

Roblox's TextWrapping does not wrap from bottom to top. What you might be observing is the Y-alignment is Center rather than Top, causing the text to start and end off the TextLabel's bounds. You should see your algorithm start to work correctly when you reduce your TextLabel's horizontal size. When you specify UDim2.new(1, 0, 0, 15), you're telling it to take up the entire horizontal space of the parent, meaning that the text already fits, meaning your algorithm won't increase the height of the label.

If you want a specific X-size, you can use TextService:GetTextSize(text, textSize, font, Vector2.new(pixels, 32767)) (you can replace the 32767 with a smaller number, but I believe it's the largest that Roblox will work with). This function will return what TextBounds will become if the TextLabel with that text/textSize/font is pixels wide.

3 comments

0

This isn't quite what I needed...

0

I typed: local chtsiz = game:GetService("TextService"):GetTextSize(cht.Text, 15, Enum.Font.SourceSansBold, Vector2.new(cht.AbsoluteSize.X, 32767)) cht.Size = UDim2.new(1,0,0,chtsiz.Y) cht.TextWrapped = true and for some reason, it didn't do what I wanted it to. What should I be doing instead?

0

I need more information. What is the value of cht.AbsoluteSize.X and what is the text you are putting into it (or just tell me what GetTextSize returned)?

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