How to Tell When A Text Box Wraps Text?

  1. What do you want to achieve? I am trying to make a script that can count the lines in a text label when TextWrapped is on.

  2. What is the issue? Whenever I try, it always says there is only one line, even when there isn’t

  3. What solutions have you tried so far? I tried doing string.split “\n” (I also want to get a list of each line)


function getLines(textLabel)
     return (textLabel.Text).split("\n")
end

label = Instance.new("TextLabel")
label.TextWrapped = true
label.Text = "Blah blah blah more text here whatever. I like cats...... hmmmmm? Idk."
--         ^ that is too much text, so the text box wraps it ^
for i, line in pairs(getLines(label)) do
    print(i .. ") " .. line)
end

That prints out:

1) "Blah blah blah more text here whatever. I like cats...... hmmmmm? Idk."

even though the text box wraps, so it should be more.

Does anyone have any ideas?

I think your looking for the TextFits property. When the text doesn’t fit anymore, the bool property is enabled.

TextService:GetTextSize() might be able to help you, as well, but there is no straightforward method. Using a monospace font will make predictions easier, but currently “Code” is the only monospace font available.

1 Like

I should back up a bit…

The text wrap displayed by GUIs with TextWrapped = true is purely visual. This operation does not modify the element’s Text property at all, which is why you can’t inherently split the string based on the element’s text. You can only achieve this result by essentially performing the same logic that the element does internally.

One way to achieve this is to do the calculation when the TextLabel.Text is set

function setTextAndTestifWrap (textLabel, text)
	textLabel.Text = text:sub(1, 1)
	local sizeY = textLabel.TextBounds.Y
	for i = 2, text:len() do
		textLabel.Text = text:sub(1, i)
		if textLabel.TextBounds.Y > sizeY then
			textLabel.Text = text
			return true
		end 
	end
	textLabel.Text = text
	return false
end

local content = "Blah blah blah more text here whatever. I like cats...... hmmmmm? Idk."
local textLabel = script.Parent
textLabel.Size = UDim2.new(0, 200, 0, 50)
textLabel.TextWrapped = true
textLabel.TextScaled = false -- this must be false for it to work
local isWrap = setTextAndTestifWrap(textLabel, content)

print(isWrap)

By modifying a little you can know how many lines it has

function setTextAndCountLines (textLabel, text)
	local counter = 0
	local sizeY = 0
	for i = 1, text:len() do
		textLabel.Text = text:sub(1, i)
		if textLabel.TextBounds.Y > sizeY then
			sizeY = textLabel.TextBounds.Y
			counter += 1
		end 
	end
	return counter
end

local content = "Blah blah blah more text here whatever. I like cats...... hmmmmm? Idk."
local textLabel = script.Parent
textLabel.Size = UDim2.new(0, 200, 0, 50)
textLabel.TextWrapped = true
textLabel.TextScaled = false -- this must be false for it to work
local numberOfLines = setTextAndCountLines(textLabel, content)

print(numberOfLines)
3 Likes