TextLabel automatically resizes based on TextBounds, font, and the size I want

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I need a function that creates textlabel with a size that fits the text

So like let’s say I do “createTextLabel(“string”, 12, Enum.Font.SourceSans)” it would return a TextLabel that is resized based on the TextBounds, TextSize, and Font.

  1. What is the issue? Include screenshots / videos if possible!
    I can’t do the automatic resizing

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    (I unfortunately already removed my code on setting the TextLabel size dynamically, and it doesn’t work)

Question, why not use the TextScaled property?

That’s not what I am trying to achieve,

  1. TextScaled does not retain the TextSize I want
  2. TextScaled is not gonna help, I’m looking for direct modification to the Size property.

Tried

function outputtext(str, color)
	local textlabel = Instance.new("TextLabel")
	textlabel.Text = str
	textlabel.Size = UDim2.new(1,0,0.1,textlabel.TextBounds.Y)
	textlabel.TextScaled = true
	textlabel.TextXAlignment = Enum.TextXAlignment.Left
	textlabel.TextYAlignment = Enum.TextYAlignment.Top
	textlabel.TextColor3 = color or Color3.fromRGB(255,255,255)
	textlabel.BackgroundTransparency = 1
	return textlabel
end

image

As you can see TextScaled doesn’t work

1 Like
function outputtext(str, color)
	local textlabel = Instance.new("TextLabel")
	textlabel.Text = str
	textlabel.Font = Enum.Font.SourceSans
	textlabel.TextSize = 16
	textlabel.TextXAlignment = Enum.TextXAlignment.Left
	textlabel.TextYAlignment = Enum.TextYAlignment.Top
	textlabel.TextColor3 = color or Color3.fromRGB(255,255,255)
	textlabel.BackgroundTransparency = 1
	textlabel.Size = UDim2.new(1,0,0.1,textlabel.TextBounds.Y)
	return textlabel
end

image

AutomaticSize seems to solve the problem partially, but if theres a lot of new-line characters then it would be cut, is there a fix for this?