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