PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

TextWrapped

TextLabel

bool

When enabled, the TextWrap property will render text on multiple lines within a UI element’s space so that TextLabel/TextBounds will never exceed the GuiBase2d/AbsoluteSize of the UI element. This is achieved by breaking long lines of text into multiple lines. Line breaks will prefer whitespace; should a long unbroken word exceed the width of the element, that word will be broken into multiple lines. If further line breaks would cause the vertical height of the text (the Y component of TextLabel/TextBounds) to exceed the vertical height of the element (the Y component of GuiBase2d/AbsoluteSize), then that line will not be rendered at all.


Code Samples


Long Text Wrapping

This code sample demonstrates TextWrap by spelling out a long chunk of text progressively. If the text doesn’t fit, it turns a different color.

local textLabel = script.Parent

-- This text wrapping demo is best shown on a 200x50 px rectangle  
textLabel.Size = UDim2.new(0, 200, 0, 50)

-- Some content to spell out
local content = "Here's a long string of words that will " ..
                "eventually exceed the UI element's width " ..
                "and form line breaks. Useful for paragraphs " ..
                "that are really long."

-- A function that will spell text out two characters at a time
local function spellTheText()
	-- Iterate from 1 to the length of our content
	for i = 1, content:len() do
		-- Get a substring of our content: 1 to i
		textLabel.Text = content:sub(1, i)
		-- Color the text if it doesn't fit in our box
		if textLabel.TextFits then
			textLabel.TextColor3 = Color3.new(0, 0, 0) -- Black
		else
			textLabel.TextColor3 = Color3.new(1, 0, 0) -- Red
		end		
		-- Wait a brief moment on even lengths
		if i % 2 == 0 then wait() end
	end
end

while true do
	-- Spell the text with scale/wrap off
	textLabel.TextWrapped = false
	textLabel.TextScaled = false
	spellTheText()
	wait(1)
	-- Spell the text with wrap on
	textLabel.TextWrapped = true
	textLabel.TextScaled = false
	spellTheText()
	wait(1)
	-- Spell the text with text scaling on
	-- Note: Text turns red (TextFits = false) once text has to be
	-- scaled down in order to fit within the UI element. 
	textLabel.TextScaled = true
	-- Note: TextWrapped is enabled implicitly when TextScaled = true
	--textLabel.TextWrapped = true 
	spellTheText()
	wait(1)
end