學習
引擎類別
TextLabel

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡


範例程式碼
遊戲狀態文字
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- 在 ReplicatedStorage 中放置一個名為 "GameState" 的 StringValue
local vGameState = ReplicatedStorage:WaitForChild("GameState")
-- 將此程式碼放在 TextLabel 中
local textLabel = script.Parent
-- 我們將使用的某些顏色和 TextColor3
local colorNormal = Color3.new(0, 0, 0) -- 黑色
local colorCountdown = Color3.new(1, 0.5, 0) -- 橙色
local colorRound = Color3.new(0.25, 0.25, 1) -- 藍色
-- 當遊戲的狀態變更時,我們將運行此函數以更新 TextLabel
local function update()
-- 更新文本
textLabel.Text = "狀態: " .. vGameState.Value
-- 根據當前的遊戲狀態設置文本顏色
if vGameState.Value == "Countdown" then
textLabel.TextColor3 = colorCountdown
elseif vGameState.Value == "Round" then
textLabel.TextColor3 = colorRound
else
textLabel.TextColor3 = colorNormal
end
end
-- 模式:當我們開始時更新一次,並且在 vGameState 變更時也更新
-- 我們應該始終看到最新的 GameState。
update()
vGameState.Changed:Connect(update)

API 參考
屬性
ContentText
唯讀
未複製
平行讀取
功能: UI
TextLabel.ContentText:string

Font
隱藏
未複製
平行讀取
功能: UI
TextLabel.Font:Enum.Font
範例程式碼
顯示所有字型
local frame = script.Parent
-- 創建一個 TextLabel 來顯示每個字型
for _, font in pairs(Enum.Font:GetEnumItems()) do
local textLabel = Instance.new("TextLabel")
textLabel.Name = font.Name
-- 設定文本屬性
textLabel.Text = font.Name
textLabel.Font = font
-- 一些渲染屬性
textLabel.TextSize = 24
textLabel.TextXAlignment = Enum.TextXAlignment.Left
-- 將框架大小 設為文本的高度
textLabel.Size = UDim2.new(1, 0, 0, textLabel.TextSize)
-- 添加到父框架
textLabel.Parent = frame
end
-- 在列表中佈局框架 (如果尚未佈局的話)
if not frame:FindFirstChildOfClass("UIListLayout") then
local uiListLayout = Instance.new("UIListLayout")
uiListLayout.Parent = frame
end
循環字型
local textLabel = script.Parent
while true do
-- 遍歷所有不同的字型
for _, font in pairs(Enum.Font:GetEnumItems()) do
textLabel.Font = font
textLabel.Text = font.Name
task.wait(1)
end
end

FontFace
平行讀取
功能: UI
TextLabel.FontFace:Font

FontSize
已棄用

LineHeight
平行讀取
功能: UI
TextLabel.LineHeight:number

LocalizedText
隱藏
唯讀
未複製
平行讀取
功能: UI
TextLabel.LocalizedText:string

MaxVisibleGraphemes
平行讀取
功能: UI
TextLabel.MaxVisibleGraphemes:number

OpenTypeFeatures
平行讀取
功能: UI
TextLabel.OpenTypeFeatures:string

OpenTypeFeaturesError
唯讀
未複製
平行讀取
功能: UI
TextLabel.OpenTypeFeaturesError:string

RichText
平行讀取
功能: UI
TextLabel.RichText:boolean

Text
平行讀取
功能: UI
TextLabel.Text:string
範例程式碼
淡出橫幅
local TweenService = game:GetService("TweenService")
local textLabel = script.Parent
local content = {
"歡迎來到我的遊戲!",
"請確保 開心玩耍!",
"請給予建議!",
"對其他玩家要友善!",
"不要對其他玩家惡搞!",
"查看商店!",
"提示:不要 死亡!",
}
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
local RNG = Random.new()
local fadeIn = TweenService:Create(textLabel, tweenInfo, {
TextTransparency = 0,
})
local fadeOut = TweenService:Create(textLabel, tweenInfo, {
TextTransparency = 1,
})
local lastIndex
while true do
-- 步驟 0:淡出再開始任何操作
fadeOut:Play()
task.wait(tweenInfo.Time)
-- 步驟 1:選擇上次未顯示的內容
local index
repeat
index = RNG:NextInteger(1, #content)
until lastIndex ~= index
-- 確保下次不顯示相同的內容
lastIndex = index
-- 步驟 2:顯示內容
textLabel.Text = content[index]
fadeIn:Play()
task.wait(tweenInfo.Time + 1)
end
文字中的表情符號
local textLabel = script.Parent
local moods = {
["happy"] = "😃",
["sad"] = "😢",
["neutral"] = "😐",
["tired"] = "😫",
}
while true do
for mood, face in pairs(moods) do
textLabel.Text = "我感覺 " .. mood .. "! " .. face
task.wait(1)
end
end

TextBounds
唯讀
未複製
平行讀取
功能: UI
TextLabel.TextBounds:Vector2
範例程式碼
動態文本框大小
local textBox = script.Parent
-- 文本框能縮小到的最小大小
local minWidth, minHeight = 10, 10
-- 設置對齊以便在輸入時文本不會有點搖晃
textBox.TextXAlignment = Enum.TextXAlignment.Left
textBox.TextYAlignment = Enum.TextYAlignment.Top
local function updateSize()
textBox.Size = UDim2.new(0, math.max(minWidth, textBox.TextBounds.X), 0, math.max(minHeight, textBox.TextBounds.Y))
end
textBox:GetPropertyChangedSignal("TextBounds"):Connect(updateSize)

TextColor
已棄用

TextColor3
平行讀取
功能: UI
TextLabel.TextColor3:Color3
範例程式碼
倒數計時文字
-- 將此代碼放在 TextLabel/TextButton 內的 LocalScript中
local textLabel = script.Parent
-- 我們將使用的一些顏色用於 TextColor3
local colorNormal = Color3.new(0, 0, 0) -- 黑色
local colorSoon = Color3.new(1, 0.5, 0.5) -- 紅色
local colorDone = Color3.new(0.5, 1, 0.5) -- 綠色
-- 無限循環
while true do
-- 從 10 倒數到 1
for i = 10, 1, -1 do
-- 設置文本
textLabel.Text = "時間:" .. i
-- 根據剩餘時間設置顏色
if i > 3 then
textLabel.TextColor3 = colorNormal
else
textLabel.TextColor3 = colorSoon
end
task.wait(1)
end
textLabel.Text = "開始!"
textLabel.TextColor3 = colorDone
task.wait(2)
end

TextDirection
平行讀取
功能: UI
TextLabel.TextDirection:Enum.TextDirection

TextFits
唯讀
未複製
平行讀取
功能: UI
TextLabel.TextFits:boolean

TextScaled
平行讀取
功能: UI
TextLabel.TextScaled:boolean

TextSize
平行讀取
功能: UI
TextLabel.TextSize:number
範例程式碼
"轟隆隆!" 文字
local textLabel = script.Parent
textLabel.Text = "轟隆隆!"
while true do
for size = 5, 100, 5 do
textLabel.TextSize = size
textLabel.TextTransparency = size / 100
task.wait()
end
task.wait(1)
end

TextStrokeColor3
平行讀取
功能: UI
TextLabel.TextStrokeColor3:Color3

TextStrokeTransparency
平行讀取
功能: UI
TextLabel.TextStrokeTransparency:number
範例程式碼
文字高亮震盪
local textLabel = script.Parent
-- 高亮應該閃爍的速度
local freq = 2
-- 設定為黃色高亮顏色
textLabel.TextStrokeColor3 = Color3.new(1, 1, 0)
while true do
-- math.sin 從 -1 震盪到 1,所以我們將範圍改為 0 到 1:
local transparency = math.sin(workspace.DistributedGameTime * math.pi * freq) * 0.5 + 0.5
textLabel.TextStrokeTransparency = transparency
task.wait()
end

TextTransparency
平行讀取
功能: UI
TextLabel.TextTransparency:number

TextTruncate
平行讀取
功能: UI
TextLabel.TextTruncate:Enum.TextTruncate

TextWrap
已棄用

TextWrapped
平行讀取
功能: UI
TextLabel.TextWrapped:boolean
範例程式碼
長文本包裝
local textLabel = script.Parent
-- 此文本包裝演示最佳效果在一個 200x50 像素的矩形上
textLabel.Size = UDim2.new(0, 200, 0, 50)
-- 一些要拼寫的內容
local content = "這是一段很長的字串,最終將會\n"
.. "超過 UI 元素的寬度 \n"
.. "並形成換行。很適合很長的段落"\n .. "你會發現。"
-- 一個每次拼寫兩個字符的函數
local function spellTheText()
-- 遍歷從 1 到我們內容的長度
for i = 1, content:len() do
-- 獲取我們內容的子字串:1 到 i
textLabel.Text = content:sub(1, i)
-- 如果文本不適合我們的框則為文本上色
if textLabel.TextFits then
textLabel.TextColor3 = Color3.new(0, 0, 0) -- 黑色
else
textLabel.TextColor3 = Color3.new(1, 0, 0) -- 紅色
end
-- 等待一個簡短的時間,當字元長度為偶數時
if i % 2 == 0 then
task.wait()
end
end
end
while true do
-- 以關閉縮放/包裝的方式拼寫文本
textLabel.TextWrapped = false
textLabel.TextScaled = false
spellTheText()
task.wait(1)
-- 以開啟包裝的方式拼寫文本
textLabel.TextWrapped = true
textLabel.TextScaled = false
spellTheText()
task.wait(1)
-- 以開啟文本縮放的方式拼寫文本
-- 注意:當文本必須縮小以適應 UI 元素時,文本會變為紅色 (TextFits = false)。
textLabel.TextScaled = true
-- 注意:當 TextScaled = true 時,自動啟用 TextWrapped
--textLabel.TextWrapped = true
spellTheText()
task.wait(1)
end

TextXAlignment
平行讀取
功能: UI
TextLabel.TextXAlignment:Enum.TextXAlignment
範例程式碼
文字對齊
-- 將這段代碼粘貼到 TextLabel/TextButton/TextBox 內的 LocalScript
local textLabel = script.Parent
local function setAlignment(xAlign, yAlign)
textLabel.TextXAlignment = xAlign
textLabel.TextYAlignment = yAlign
textLabel.Text = xAlign.Name .. " + " .. yAlign.Name
end
while true do
-- 遍歷 TextXAlignment 和 TextYAlignment 枚舉項目
for _, yAlign in pairs(Enum.TextYAlignment:GetEnumItems()) do
for _, xAlign in pairs(Enum.TextXAlignment:GetEnumItems()) do
setAlignment(xAlign, yAlign)
task.wait(1)
end
end
end

TextYAlignment
平行讀取
功能: UI
TextLabel.TextYAlignment:Enum.TextYAlignment

©2026 Roblox Corporation、Roblox、Roblox 標誌及 Powering Imagination 是我們在美國及其他國家地區的部分註冊與未註冊商標。