Help with scaling text labels font size for a shop gui

Hi guys! I am scripting a shop Gui that creates the categories and image buttons from a large table I created storing all the information that is needed. Inside each image button I have a text label that shows the price of the object.The only issue I have is scaling the font sizes appropriately. I dont want to used text scale as it would not look professional as text displayed would not be homogeneous. So I was wondering if there is anyway for me to look at all the sizes of each price string and come up with an appropriate font size so all of them are the same size.

4 Likes

Can you try making the textbox smaller and enabling text scale?

I said i cant use text scale because the text would not all have the same font size

I know but if you’re making the textbox smaller, it may still look appropriate :slight_smile:

that not a feasible method I am making the Gui through a script not physically, because it would make updates easier

TextService has a GetTextSize function to calculate the area needed to display a string at a certain font and size within a given space.

3 Likes

how is that relevant to my issue? I want a method to calculate the font size because I don’t want to use text scaled since the strings are not equal in length

Create an image with the text rather than using a text box. This way you can set the sizes when you are making them image, and everything will scale properly together.

If you wanted to have a consistent text size, then try taking the object’s .AbsoluteSize.Y value. If all of your buttons are the same height, this will work.

I don’t know if this checks all of your boxes, but here’s a crude example:

Hierarchy

Hierarchy

Code

local TextService = game:GetService("TextService")

local Template = script.ItemFrame
	local TextLabel = Template.Price

local Shop = script.Parent
	local Container = Shop.Container

local Size = 24 -- Initialize with max preferred size
local Prices = {100, 3000, 75000}

local function NormalizeTextSize()
	for i, v in pairs(Prices) do
		local Area = TextService:GetTextSize(tostring(v), Size, Enum.Font.SourceSansBold, Vector2.new(500, 500))
		-- The last argument determines wrapping, so we can throw in a large, arbitrary Vector2 to ignore it
		
		if TextLabel.AbsoluteSize.X < Area.X or TextLabel.AbsoluteSize.Y < Area.Y then -- Check if the space available is less than the space required
			Size = Size - 1
			return NormalizeTextSize()
		end
	end
end

NormalizeTextSize()

-- Generate your shop items
for i, v in pairs(Prices) do
	local Item = Template:Clone()
	Item.Price.TextSize = Size
	Item.Price.Text = v
	Item.Parent = Container
end
1 Like

This seems like it will work perfectly thanks much :slight_smile:

your solution is not working as expected, the font size is way too big

Could I see how you’re applying it?

Here is the model file for the entire example: TextScaling.rbxm (4.8 KB)

can u upload it in another format please i cant open that

Sure, here’s the place file: TextScaling.rbxl (19.0 KB)

I see what i was doing wrong thanks for the help :slight_smile:

2 Likes