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

How would I make a textlabel's X offset coordinate match the TextBound's X size?

Asked by 9 years ago

I've seen a game do this before but I'm not quite sure how to do it myself.

How would I match a textlabel's size {0,0}, {0,0}match a TextBound size 0,0

For example, if the textbound was 164,36 how could I have a script make the X (or Y if necessary) be a size according to TextBound ( {0,164}, {0,36} )?

Thank you!

P.S. I wasn't really sure about the best way to word this question so if you need any clarifications, please let me know.

2 answers

Log in to vote
1
Answered by
drahsid5 250 Moderation Voter
9 years ago

TextLabel.Position is is UDim2 value. Inside a UDim2 are two Vector2 values, one for X, one for Y. The first value in the Vector2s' is the Scale (Relative to the % of the screen resolution, so if you have a 1920x1080 screen and your X scale is .5 (%50) it would be 960 pixels) The second value is the Offset (Size in Pixels)

So doing this is quite simple, First you find the TextLabel and TextBound.

local TLab = script.Parent.TLab
local TBnd = script.Parent.OtherTLab

Next we want to make a function that has the values as arguments.

function matchSize(TLab,TBnd) -- Function name & Arguments

end

matchSize(TLab,TBnd) --Calls the function

Simple, right?

Now, in the function we want to define the size of the TextBound,

nSize = TBnd.Size --Creates a Vector2 that contains the same values as the TextBound's size X and Y

Okay, we're almost done, now we change the Offset to the new size

TLab.Size.Offset = nSize --Set the Offset to the the same value as nSize

and now We're done, the code should look like this:

local TLab = script.Parent.TLab
local TBnd = script.Parent.OtherTLab

function matchSize(TLab,TBnd)
    nSize = TBnd.TextBounds
   TLab.Size = UDim2.new(TLab.Size.X.Scale,nSize.X,TLab.Size.Y.Scale,nSize.Y)
end

matchSize(TLab,TBnd)

If you have any issues, don't hesitate to tell me I made a mistake!

Edit: I made a mistake with what TextBound was, but I fixed it.

Edit2: Apparently Size.Offset is read only, (Can't be changed) so I changed it to

TLab.Size = UDim2.new(TLab.Size.X.Scale,nSize.X,TLab.Size.Y.Scale,nSize.Y)
Ad
Log in to vote
1
Answered by 9 years ago

The TextBounds have an X and Y value. These values will make it really easy to change its size. When the TextBox changes, change its size to an UDim2 with the offsets being the X and Y of the TextBounds.

If you didn't understand anything from above, you can always just use the below code:

TextBox.Changed:connect(function()
    TextBox.Size = UDim2.new(0, TextBox.TextBounds.X, 0, TextBox.TextBounds.Y)
end)

Answer this question