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

Resize TextLabel to fit text?

Asked by 10 years ago

EDIT:It appears TextBounds doesn't work at all with plugins. I may file a bug report. In the mean time, anyone else have some ideas to get around this?

I'm creating tooltips which need to expand to fit the size of the given text. I know TextBounds is supposed to give the size of the text, but I'm finding it to be (0,0). This is a part of a plugin, so the TextLabels are a part of the CoreGui (inside a screengui, frame, etc.).

I need to figure out why TextBounds isn't working. Either that or I need to write a function to calculate it( meaning I'll have to figure out the size of each character).

Here's the relevant code

function Tooltip(text, parent)
    local pad = 4
    local tooltip = Create("TextLabel", {
        BackgroundColor3 = Color3.new(1,1,1);
        BorderSizePixel = 0;
        BorderColor3 = Color3.new(1,1,1);
        Text = text;
        ZIndex = 5;
        Parent = parent;
        Create("Frame", { --drop shadow
            BackgroundColor3 = Color3.new(1,1,1);
            BackgroundTransparency = 0.5;
            Size = UDim2.new(1,0,1,0);
            Position = UDim2.new(0,3,0,3);
            ZIndex = 4;
        })

    })
    wait()--shouldn't have to, but it was possible TextBounds needed a moment.
    print(tooltip.TextBounds)
    tooltip.Size = UDim2.new(0,tooltip.TextBounds.X + pad,0,tooltip.TextBounds.Y + pad)
    return tooltip
end

--And if you need it the Create function. Likely written by Anaminus since I was borrowing
some of his code which used it.

function Create(ty,data)
    local obj
    if type(ty) == 'string' then
        obj = Instance.new(ty)
    else
        obj = ty
    end
    for k, v in pairs(data) do
        if type(k) == 'number' then
            v.Parent = obj
        else
            obj[k] = v
        end
    end
    return obj
end

Debug info:

 o tooltip.Text is being set - confirmed by a print
 o tooltip is being parented to CoreGui.ScreenGui.ui.panel
 o waiting for tooltip.TextBounds.X to be greater than 0 results in an infinite loop.
 o Setting tooltip.Size does not effect TextBounds
0
That's a nice Create function that I may just steal, but that's not where your error is. Do you still bug when you give it some hard-coded text instead of what you passed as an argument? adark 5487 — 10y
1
Same results when not using the Create function and giving it pre-set text. MrgamesNwatch 65 — 10y

1 answer

Log in to vote
-1
Answered by 10 years ago

a TextLabel has TextBounds so if you use those you should get it.

Example.

TextLabel.Size = UDim2.new(0, TextLabel.TextBounds.X, 0, TextLabel.TextBounds.Y)
0
I'm already using TextBounds as I mentioned. The problem is TextBounds property not being set. MrgamesNwatch 65 — 10y
0
you cant set a TextBound property it is read only DragonSkyye 517 — 10y
0
Yes I know, but TextBounds appears to indefinitely be (0,0). MrgamesNwatch 65 — 10y
Ad

Answer this question