TextBounds - How to get the size in Scale to apply it onto a TextBox?

Working on the UI which generates subcategories for each category. However, some subcategories have longer names (10 chars) compared to some other ones (4-5 chars), ruining the aesthetic and leaving some TextBoxes separated unevenly.

Code:

local button = Instance.new("TextButton")
button.BackgroundTransparency = 1
button.TextColor3 = Color3.new(200,205,206)
button.Size = UDim2.new(0.109,0,0.992,0)
button.Name = subvalue; button.Text = subvalue
button.TextSize = 14; button.TextScaled = true; button.TextWrapped = true; button.Font = Enum.Font.GothamSemibold; button.TextYAlignment = Enum.TextYAlignment.Center; button.TextXAlignment = Enum.TextXAlignment.Center
button.Parent = UI.Catalog.Subcategories
local TextBoundsX = button.TextBounds.X
local TextBoundsY = button.TextBounds.Y
local ViewPortSize = workspace.Camera.ViewportSize
button.Size = UDim2.new(TextBoundsX / ViewPortSize.X, 0, TextBoundsY / ViewPortSize.Y, 0)

I know I can use TextBounds, but how do I convert the TextBox’s size to Scale? Thanks!

I’m not sure if this will help but, there is a ScaledText property in basically any UI with Text such as TextLabel. Hope this helps. :wink:

Oh, I kinda wasn’t specific, sorry! Made an edit to the thread.

Basically, I need the size of the TextBox to adjust to the size of the characters in Text, but using TextBounds returns values in Offset.

Update: Here’s the code I’m currently using and it although it returns no errors, it scales everything wrong.

Code:

local button = Instance.new("TextButton")
button.BackgroundTransparency = 1
button.TextColor3 = Color3.new(200,205,206)
button.Size = UDim2.new(0.109,0,0.992,0)
button.Name = subvalue; button.Text = subvalue
button.TextSize = 14; button.TextScaled = true; button.TextWrapped = true; button.Font = Enum.Font.GothamSemibold; button.TextYAlignment = Enum.TextYAlignment.Center; button.TextXAlignment = Enum.TextXAlignment.Center
button.Parent = UI.Catalog.Subcategories
local TextBoundsX = button.TextBounds.X
local TextBoundsY = button.TextBounds.Y
local ViewPortSize = workspace.Camera.ViewportSize
button.Size = UDim2.new(TextBoundsX / ViewPortSize.X, 0, TextBoundsY / ViewPortSize.Y, 0)

You can use the TextFits property to detect when the text fits inside the TextLabel:

local label = script.Parent.TextLabel 

function ScaleText(textObject, offset)
	if not textObject.TextFits then 
		repeat 
			textObject.TextSize -= 1
		until textObject.TextFits 
	end
	textObject.TextSize -= offset or 0
end

ScaleText(label, 5)
2 Likes

I’m not looking to scale the text down; I need to scale the TextBox depending on the TextBounds of the text itself.

However, it returns an Offset value and, as seen by the code above, I’m trying to convert it to Scale unsuccessfully.

This is working for me:

local UI = script.Parent --a screen UI
local label = UI.TextLabel 

label.TextWrapped = false --must be set to false for the actual text size
local ScreenSize = UI.AbsoluteSize 
local textOffset = label.TextBounds 

local textScale = UDim2.fromScale(textOffset.X/ScreenSize.X, textOffset.Y/ScreenSize.Y)

label.Size = textScale+UDim2.fromScale(0.02, 0.02) --ExactSize+some offset

I assume the issue is related to button.TextWrapped = true showing you false results.

1 Like

Current result: ( :rofl: :rofl: :rofl:)

1 Like

Let me try it real quick. Be back in a sec.

Still the same thing, not sure what I’m doing wrong.

Important inputs:

local CatalogSizeReference = UI.Catalog.AbsoluteSize
local TextBoundsX = button.TextBounds.X
local TextBoundsY = button.TextBounds.Y
button.Size = UDim2.fromScale(TextBoundsX/CatalogSizeReference.X, TextBoundsY/CatalogSizeReference.Y)

Basically, what your script does is, it still messes up the sizing and makes it extremely small although the text shows properly, but I can’t have the TextScaled set to off; I want this to match other device resolutions.

UPDATE: Managed to sort it out. Thanks for the help!

Instead of using the Catalog frame size, use the ScreenGui size.

Yeah; I realized I needed to use a different frame for a realistic ratio. Thanks!