How do I find lines of text?

find_The_Text
I only want the bottom line of text (preferably I don’t have to create a GUI to do this). TextService only gives me text size so I can’t use it :sob:

I already tried this, and it doesn’t work (because the lines were created with TextWrapped). It returns the whole thing, instead of splitting it into pieces. This also wouldn’t work when I am working with TextService

Try splitting with the " " (space) and then combining the table data to obtain the string.

Edit: (Obviously inefficient and can’t be used in all the cases)

He is trying to find a line, not the entire string.

Is it like there is no space between words? Also see my edit.

Also, true this can’t be used in all the cases and I’m not thinking he in only writing two words so this will be inneficient.

1 Like

Trying to split using all whitespace characters (%s represents whitespace; the " ", “\n” are all whitespace) also doesn’t work.

1 Like

It seems like you’d have to remove text until GetTextSize returns a lower Y value. Here’s a quick mockup of what it might look like (untested, but should give a general idea):

local label = -- wherever the textlabel is
local textService = game:GetService("TextService")
local previousSize = Vector2.new()
local newLines = {}

for i = #label.Text, 1, -1 do
	local size = textService:GetTextSize(label.Text:sub(1, i), --[[your font size]], label.Font, label.AbsoluteSize)
	if size.Y < previousSize.Y then
		table.insert(newLines, i + 1) -- since the size decreased, last char was the start of a new line
	end

	previousSize = size
end

This might act weirdly when lines break early because of a space, but I’m not sure there’s a better way to do this.

4 Likes

That’s very inefficient, you should get the whole text size, calculate how many lines you have, then using that get the (average) amount of characters in a single line and finally using that get the index of last/first character in a line.

This is how you’d do that. How else would you do it? You can’t just divide, because not all lines will be equal in length. (For example, WWWWW is a lot longer than iiiii even though it’s the same number of characters.)

2 Likes

You can remove characters from the end of the text until the number of lines decreases by one, then divide by the amount of lines which gives you the average amount of characters in a line. Now iterate from the beginning and using the average amount of characters optimize the algorithm of finding the end of a line, now you know how many characters are there exactly in that line. Having that done you can optimize the search for the next line and so on.

That sounds super hacky and unreliable. If you can come up with a better implementation, go ahead, but it seems like finding the average number of characters per line won’t do much for you.

3 Likes

It is definitely going to make the algoritm go from O(n * m) efficiency to O(n) where n is the number of lines and m is the average number of characters in that line.

That’s not how big O notation works.

2 Likes

So maybe you will explain us how it works if you assume that I am wrong? xd

You don’t base it off the best case scenario. Your algorithm won’t find the split in the line on the first GetTextSize call when almost every line is going to be different lengths.

3 Likes

It might be possible to get the number of lines with some math, but it is impossible to get the text accurately just using the average # of characters (they wouldn’t work since one line can deviate significantly from the mean, so some text might get cut off).

The original solution proposed by @posatta would probably work (even if horribly inefficient) if we accounted for whitespace, and use other small tricks. If anyone has a better way of doing this, please tell us!

1 Like

If efficiency becomes a big issue, you could try iterating by a larger increment (-10, for example) and only going more precise once you find the 10 character range that the new line is a part of. I’m not sure how inefficient GetTextSize is, but I doubt it’ll be a big issue that it’s terribly optimized.

Edit: Of course, that brings up the potential problem of having multiple new lines per 10 characters.

3 Likes