Making text the same size with TextSecaled enabled

Hello everyone, Im making a donation UI but because of using TextScaled I cannot make the text in every button the same size.
Here’s what I mean:
image

For example, the 10000 robux text is a lot smaller than the 10 robux one.

Anyone know how to fix this?

Thanks for reading!

2 Likes

you should turn on text warped instead of text scaled so you can change the size of the texts

3 Likes

The problem is that the text doesnt scale for different devices:


3 Likes

You can use AutoScale Plug-in do add UI aspect ratio or to convert their size and position to scale

3 Likes

You could use TextService:GetTextSize with the size of each button. You’d have to loop through each button and increment the text size until it no longer fits within the button’s bounds.

If you store, across that loop, the smallest font size just before it exceeds the dimensions, then you can set them all to that.

Unfortunately I don’t think there’s any better way to currently synchronise auto scaled text.

local TextService = game:GetService( 'TextService' )
local validSize = 0
for size = 1, 100 do
    local isValid = true
    for _, button in ipairs( yourButtons ) do
        local bounds = TextService:GetTextSize( button.Text, size, button.Font, Vector2.new( button.AbsoluteSize.X, math.huge ) )
        if bounds.X + 1 > button.AbsoluteSize.X or bounds.Y + 1 > button.AbsoluteSize.Y then
            isValid = false
            break
        end
    end
    if isValid then
        validSize = size
    else
        break
    end
end
for _, button in ipairs( yourButtons ) do
    button.TextSize = validSize
    button.TextScaled = false
end
3 Likes

There’s actually a simple, but smart trick that does not require scripting. For every textbox, set the anchor point to 0, 0.5 and set the size to something like 1, 0, 1, -10. Assuming that the textbox is parented to a frame.

2 Likes

Doesnt work for some reason

local TextService = game:GetService( 'TextService' )
local q = game.StarterGui.BetaWelcome.Frame:FindFirstChild("10").TextLabel1
local w = game.StarterGui.BetaWelcome.Frame:FindFirstChild("100").TextLabel1
local e = game.StarterGui.BetaWelcome.Frame:FindFirstChild("1000").TextLabel1
local r = game.StarterGui.BetaWelcome.Frame:FindFirstChild("10000").TextLabel1
local t = game.StarterGui.BetaWelcome.Frame:FindFirstChild("20").TextLabel1
local y = game.StarterGui.BetaWelcome.Frame:FindFirstChild("200").TextLabel1
local u = game.StarterGui.BetaWelcome.Frame:FindFirstChild("450").TextLabel1
local i = game.StarterGui.BetaWelcome.Frame:FindFirstChild("50").TextLabel1
local o = game.StarterGui.BetaWelcome.Frame:FindFirstChild("5000").TextLabel1
local tab = {q,w,e,r,t,y,u,i,o}
local validSize = 0
for size = 1, 100 do
    local isValid = true
    for _, button in ipairs(tab) do
        local bounds = TextService:GetTextSize( button.Text, size, button.Font, button.AbsoluteSize )
        if bounds.X + 1 > button.AbsoluteSize.X or bounds.Y + 1 > button.AbsoluteSize.Y then
            isValid = false
            break
        end
    end
    if isValid then
        validSize = size
    else
        break
    end
end
for _, button in ipairs( yourButtons ) do
    button.TextSize = validSize
end

image

You forgot to swap the second yourButtons - the one that actually sets the text size at the bottom.

1 Like

Used this, no errors appear but nothing happens:

local TextService = game:GetService( 'TextService' )
local q = game.StarterGui.BetaWelcome.Frame["10"].TextLabel1
local w = game.StarterGui.BetaWelcome.Frame["100"].TextLabel1
local e = game.StarterGui.BetaWelcome.Frame["1000"].TextLabel1
local r = game.StarterGui.BetaWelcome.Frame["10000"].TextLabel1
local t = game.StarterGui.BetaWelcome.Frame["20"].TextLabel1
local y = game.StarterGui.BetaWelcome.Frame["200"].TextLabel1
local u = game.StarterGui.BetaWelcome.Frame["450"].TextLabel1
local i = game.StarterGui.BetaWelcome.Frame["50"].TextLabel1
local o = game.StarterGui.BetaWelcome.Frame["5000"].TextLabel1
local tab = {q,w,e,r,t,y,u,i,o,}
local validSize = 0
for size = 1, 100 do
    local isValid = true
    for _, button in ipairs(tab) do
        local bounds = TextService:GetTextSize( button.Text, size, button.Font, button.AbsoluteSize )
        if bounds.X + 1 > button.AbsoluteSize.X or bounds.Y + 1 > button.AbsoluteSize.Y then
            isValid = false
            break
        end
    end
    if isValid then
        validSize = size
    else
        break
    end
end
for _, button in ipairs(tab) do
    button.TextSize = validSize
end

Do you mind uploading a quick repro place with the GUI. I’d like to run it and see if I can identify the issue for you. You can remove anything unrelated to those buttons/labels.

1 Like

uithing.rbxl (26.2 KB)

Okay, so I forgot to mention you need to disable TextScaled otherwise it ignores the TextSize we set.

Secondly, I noticed an issue due to text wrapping and strange TextService behaviour, so swap the GetTextSize line to this:

        local bounds = TextService:GetTextSize( button.Text, size, button.Font, Vector2.new( button.AbsoluteSize.X, math.huge ) )
1 Like

Now it says this image

Minor typo - swap Vector3 for Vector2 on that same line.

1 Like

Now no errors appear, but doesn’t seem to do anything.

It worked nicely in studio for me using your demo place and the code that I’ve updated as of the last few replies. Check each label after running to see if they all have a common TextSize or not as a check.

1 Like

Yeah, but the text still changes size depending on the screen size.

If they have the same text size but it still auto scales without you doing anything then TextScaled must be set to true. Set it to false as I mentioned before, or include it in the script such as in my edited reply with the code.