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

Text

TextLabel

string

The Text property determines the content rendered by the UI element. The visual properties of the string rendered to the screen is determined by TextLabel/TextColor3, TextLabel/TextTransparency, TextLabel/TextSize, TextLabel/Font, TextLabel/TextScaled, TextLabel/TextWrapped, TextLabel/TextXAlignment and TextLabel/TextYAlignment.

It is possible to render emoji (for example, 😃) and other symbols. These special symbols aren’t affected by the TextLabel/TextColor3 property. These can be pasted into Script and LocalScript objects, as well as the field within the Properties window.

This property may contain newline characters, however, it is not possible to type newline characters within the Properties window. Similarly, this property may contain a tab character, but it will render as a space instead.


Code Samples


Emoji in Text

This code sample demonstrates emoji rendering using the Text property.

local textLabel = script.Parent

local moods = {
	["happy"] = "😃";
	["sad"] = "😢";
	["neutral"] = "😐";
	["tired"] = "😫";
}

while true do
	for mood, face in pairs(moods) do
		textLabel.Text = "I am feeling " .. mood .. "! " .. face
		wait(1)
	end
end

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

"Kaboom!" Text

This code sample repeatedly tweens a TextLabel’s TextSize from 5 to 100 and fades out the text as it grows in size.

local textLabel = script.Parent

textLabel.Text = "Kaboom!"

while true do
	for size = 5, 100, 5 do
		textLabel.TextSize = size
		textLabel.TextTransparency = size / 100
		wait()
	end
	wait(1)
end

Fading Banner

This code sample creates a fading banner for a TextLabel. It fades text out, chooses a random string (avoiding repetition), and fades back in.

local textLabel = script.Parent

local content = {
	"Welcome to my game!";
	"Be sure to have fun!";
	"Please give suggestions!";
	"Be nice to other players!";
	"Don't grief other players!";
	"Check out the shop!";
	"Tip: Don't die!";
}

local function fadeOut()
	for i = textLabel.TextTransparency, 1, .1 do
		wait(.1)
		textLabel.TextTransparency = i
	end
end

local function fadeIn()
	for i = textLabel.TextTransparency, 0, -.1 do
		wait(.1)
		textLabel.TextTransparency = i
	end
end

local lastIndex
while true do
	-- Step 0: Fade out before doing anything 
	fadeOut()
	
	-- Step 1: pick content that wasn't the last displayed
	local index	
	repeat
		index = math.random(1, #content)
	until lastIndex ~= index
	-- Make sure we don't show the same thing next time
	lastIndex = index
	
	-- Step 2: show the content
	textLabel.Text = content[index]
	fadeIn()
	wait(2)
end

Show All Fonts

This code sample renders a list of all the available fonts.

local frame = script.Parent

-- Create a TextLabel displaying each font
for i, font in pairs(Enum.Font:GetEnumItems()) do
	local tl = Instance.new("TextLabel")
	tl.Name = font.Name
	-- Set the text properties
	tl.Text = font.Name
	tl.Font = font
	-- Some rendering properties
	tl.TextSize = 24
	tl.TextXAlignment = Enum.TextXAlignment.Left
	-- Size the frame equal to the height of the text
	tl.Size = UDim2.new(1, 0, 0, tl.TextSize)
	-- Add to the parent frame
	tl.Parent = frame
end

-- Layout the frames in a list (if they aren't already)
if not frame:FindFirstChildOfClass("UIListLayout") then
	Instance.new("UIListLayout", frame)
end