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

How do I use TextLabel.TextBounds?

Asked by 6 years ago

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".

0
Why not use `TextScaled`? TheeDeathCaster 2368 — 6y
0
Why not just make the text box smaller? hiimgoodpack 2009 — 6y
0
Using TextScaled would make the text smaller, which I don't want, and making the text box smaller would be counterproductive. iiPostMaster 31 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

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.

0
This isn't quite what I needed... iiPostMaster 31 — 6y
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? iiPostMaster 31 — 6y
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)? chess123mate 5873 — 6y
Ad

Answer this question