I just learned about that, but I’m not sure if I should use them or not, since it doesn’t seem like it’s going to be much more efficient or easy.
This currently works quite well for me, but it’s not really the most efficient; around 1% script usage for me when vigorously changing the resolution, and that’s with only 2 text label groups, but I think it should be OK since a small performance hit can generally be ignored when you’re resizing your game for half a second.
Would appreciate thoughts on if there’s a better way to do this though
Example place Smart Text Scaler.rbxl (20.2 KB)
Module modelSmartTextScaler.rbxm (2.8 KB)
Text scaling with module (updates with resolution):
Regular TextScaled:
Here is the module;
--[[
SmartTextHandler module by Bobby_Darin
This simulates the TextScaled property for TextButtons and TextLabels,
but allows you to set a "group" of TextButtons and TextLabels
which will all scale to the same size (the largest size possible,
without any one member of the group having a text size too big for
their gui object).
By default, the TextSize will update every time the game resolution
is changed by the player.
This works by finding the longest TextLabel/TextButton of the group,
and then finding the largest possible text size for this TextObject
(without the text escaping the bounds of the gui container),
and then setting all the TextObjects in the group to that size.
To use, Module:NewGroup(object : table)
The object dictionary argument should consist of;
Required properties
array TextObjects : all TextLabels and TextButtons in the same
"group," which will all maintain the same text size whenever
the resolution is changed.
Optional properties
bool OnlyUpdateWhenVisible (false)
When the game resolution is changed, the text group
size will only update if at least one member of
TextObjects is visible.
bool UpdateWhenMadeVisible (false)
Update the text group size whenever a member of
TextObjects is made visible, in addition to
whenever the resolution is changed
int MinTextSize (1)
int MaxTextSize(1)
Vector2 TextBorderSize (2, 2)
number SizeModifier (0.95)
Example usage:
----
Module:NewGroup{
TextObjects = {
TextLabel1,
TextLabel2,
TextButton1
},
MaxTextSize = 50,
}
----
TextLabel1, TextLabel2, and TextButton1 will now be automatically scaled,
and all maintain the same text size too.
--]]
------------------------------------------
local SmartTextHandler = {}
local text_service = game:GetService("TextService")
local GetTextSize = text_service.GetTextSize
------------------------------------------
-- Methods
function SmartTextHandler:NewGroup(object)
if not object then
return
end
if not object.TextObjects then
return
end
setmetatable(object, self)
self.__index = self
object.MinTextSize = object.MinTextSize or 1
object.MaxTextSize = object.MaxTextSize or 100
object.TextBorderSize = object.TextBorderSize or Vector2.new(2, 2)
object.SizeModifier = object.SizeModifier or 0.95
object:Update()
if object.UpdateWhenMadeVisible then
for k,text_object in next, object.TextObjects do
text_object.Changed:connect(function(property)
if property ~= "Visible" or not text_object.Visible then
return
end
object:Update()
end)
end
end
workspace.CurrentCamera.Changed:connect(function(property)
if property ~= "ViewportSize" then
return
end
-- property == "ViewportSize"
if not object.OnlyUpdateWhenVisible then
object:Update()
return
end
-- object.OnlyUpdateWhenVisible == true
for k,text_object in next, object.TextObjects do
if text_object.Visible then
-- a TextObject is visible, so update the group
object:Update()
return
end
end
end)
return object
end
function SmartTextHandler:GetLongestTextObject()
local longest_object = nil
local longest_object_str_length = 0
for k,text_object in next, self.TextObjects do
if #text_object.Text > longest_object_str_length then
longest_object = text_object
longest_object_str_length = #text_object.Text
end
end
return longest_object
end
function SmartTextHandler:Update()
local best_size = 0
-- Text sizing is based off of the TextObject with
-- the most characters, as this object will have a lower possible
-- font size than the rest of the group
local longest_object = self:GetLongestTextObject()
local text = longest_object.Text
local font = longest_object.Font
local bounds = (longest_object.AbsoluteSize - self.TextBorderSize) * self.SizeModifier
local wrap_size = Vector2.new(bounds.X, 1e4)
for font_size = self.MinTextSize + 1, self.MaxTextSize + 1 do
local text_size = GetTextSize(
text_service,
text,
font_size,
font,
wrap_size
)
if text_size.X >= bounds.X or text_size.Y >= bounds.Y then
best_size = font_size - 1
break
end
end
for k,text_object in next, self.TextObjects do
text_object.TextScaled = false
text_object.TextWrapped = true
text_object.TextSize = best_size
end
if self.OnUpdate then
self:OnUpdate()
end
end
return SmartTextHandler