TextService:GetTextSize not working as expected

I am making a ‘rich text’ module, which currently just fades in one character at a time by creating a text label for each character, and doing whatever with it. This works well with one line, but TextService:GetTextSize seems to abruptly break whenever the text is about to wrap over to the next line.
How it currently looks:


As you can see, it works fine on the first line but then the characters get stuck in the same position.

Here’s an example script:

local String = ('hello world'):rep(10);
local fullText = ''
local textService = game:GetService('TextService');
for idx = 1, #String do
    fullText = fullText .. String:sub(idx, idx)
    print(textService:GetTextSize(fullText, 5, Enum.Font.Garamond, Vector2.new(100, 100)));
end;

Now, let’s check the output:
image
As you can see the text reaches up to 100, 5 just fine, but when it wraps to the next line the X value is just permanently stuck at 84 (Y axis seems to be fine).
Now, you could go around this by running the same thing again recursively and add up each result to simulate text wrapping, but that would be messy and wouldn’t fix the issue either way.
I tried messing around with the parameters (especially the last one, text size), but it didn’t seem to do anything.
I’d like to find out what the issue is, and if possible, fix it. Thank you!

3 Likes

can we see the scipt that actually puts the text onto the Gui?

1 Like
Module.writeText = function(self, Text, Label)
	local offsetBounds = textService:GetTextSize('', Label.TextSize, Label.Font, Label.AbsoluteSize);
	local fullText = '';
	local textLen = #Text;
	local textPos = Vector2.new();
	
	local yOffset = Vector2.new(0, offsetBounds.Y);
	for char_count = 1, textLen do
		local currentCharacter = Text:sub(char_count, char_count);
		fullText = fullText .. currentCharacter;
		local textBounds = textService:GetTextSize(fullText, Label.TextSize, Label.Font, Label.AbsoluteSize);
		local charBounds = textService:GetTextSize(currentCharacter, Label.TextSize, Label.Font, Label.AbsoluteSize);
		textPos = textBounds - yOffset;

		Module:addCharacter(Label, charBounds, Vector2.new(textPos.X - charBounds.X, textPos.Y), currentCharacter);
		renderStepped:Wait();
	end;
end;

addCharacter should be irrelevant, as it just sets the properties of the character label etc.

1 Like

Actually, this happens since the size returned is the size that u would use to size a frame.

3 Likes

Ah, yea I see the issue. Thank you!

1 Like