Help with resizing textlabel size with :GetTextSize()

I am creating a custom chat and I’m trying to get the Y size of the text using :GetTextSize() so I can scale the textlabel Y size and make it fit.

The issue is whenever I print bounds.Y it always returns as 25 no matter what

(I do not want to use roblox’s new automatictextsize, please don’t suggest it.)

function createnewtext(text)
	local textlabel = Instance.new("TextLabel")
	textlabel.BackgroundTransparency = 1
	textlabel.Position = UDim2.new(0,0,1,0)
	textlabel.Size = UDim2.new(1,0,0,25)
	textlabel.Font = Enum.Font.Gotham
	textlabel.TextColor3 = Color3.fromRGB(255,255,255)
	textlabel.Text = text
	textlabel.TextXAlignment = Enum.TextXAlignment.Left
	textlabel.TextSize = 25
	textlabel.TextWrapped = true
	local bounds = TextService:GetTextSize(textlabel.Text,textlabel.TextSize,textlabel.Font,Vector2.new(textlabel.AbsoluteSize.X,textlabel.AbsoluteSize.Y))
	textlabel.Size = UDim2.new(1,0,0,bounds.Y)
    print(bounds)
	textlabel.Parent = textframe
end

image
image

2 Likes

Well that is simply because you are setting textsize to 25. Text size is actually describing the height of the text. When I change the value of the test height your code works flawlessly. I think it might be your parent frame being sized different that’s throwing you off. Try setting BackgroundTransparency to 0 for your testing.

Basically I want my message to go like this whenever the text reaches the end of the frame (basically the textlabel.Y just gets bigger) but my bounds always print 25 for the Y) is there a fix for this or am I doing something wrong?
image

1 Like

Two things

You need to set the parent first before using absolute size. Without doing that it will assume 0 for anything using scale. If it gets less than the size of a word or character it will default to one line it seems.

And you need to make the text box the max size it can be on the Y axis because it will clip it to the full size of the box. So when you make the textbox by default 25, it will only allow it to go to 25.

function createnewtext(text)
	local textlabel = Instance.new("TextLabel")
	textlabel.BackgroundTransparency = 1
	textlabel.Position = UDim2.new(0,0,1,0)
	textlabel.Size = UDim2.new(1,0,1,0)
	textlabel.Font = Enum.Font.Gotham
	textlabel.TextColor3 = Color3.fromRGB(255,255,255)
	textlabel.Text = text
	textlabel.TextXAlignment = Enum.TextXAlignment.Left
	textlabel.TextSize = 25
	textlabel.TextWrapped = true
    textlabel.Parent = textframe
	local bounds = TextService:GetTextSize(textlabel.Text,textlabel.TextSize,textlabel.Font,Vector2.new(textlabel.AbsoluteSize.X,textlabel.AbsoluteSize.Y))
	textlabel.Size = UDim2.new(1,0,0,bounds.Y)
    print(bounds)
end
5 Likes

Thank you, this worked! I’ll mark this as the solution.

1 Like