เรียนรู้
Engine Class
TextLabel

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่


สรุป
สมาชิกที่ได้รับทอด
ตัวอย่างโค้ด
ข้อความสถานะเกม
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- วาง StringValue ที่เรียกว่า "GameState" ใน ReplicatedStorage
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
-- คุณสมบัติการเรndering บางอย่าง
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
ตัวอย่างโค้ด
ขนาด TextBox แบบไดนามิก
local textBox = script.Parent
-- ขนาดเล็กที่สุดที่ TextBox จะไปได้
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
ตัวอย่างโค้ด
ข้อความนับถอยหลัง
-- วางโค้ดนี้ใน LocalScript ภายใน TextLabel/TextButton
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
ตัวอย่างโค้ด
"Kaboom!" ข้อความ
local textLabel = script.Parent
textLabel.Text = "Kaboom!"
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 px
textLabel.Size = UDim2.new(0, 200, 0, 50)
-- เนื้อหาบางส่วนที่จะแสดงออก
local content = "นี่คือข้อความยาวๆ ที่จะ "
.. "เกินความกว้างของ UI element "
.. "และจะทำให้เกิดการขึ้นบรรทัดใหม่ เป็นประโยชน์สำหรับย่อหน้าที่ "
.. "ยาวจริงๆ."
-- ฟังก์ชันที่จะแสดงข้อความทีละสองตัวอักษร
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)
-- แสดงข้อความโดยเปิดการปรับขนาด
-- หมายเหตุ: ข้อความจะเปลี่ยนเป็นสีแดง (TextFits = false) เมื่อต้อง
-- ลดขนาดให้พอดีกับ UI element.
textLabel.TextScaled = true
-- หมายเหตุ: TextWrapped จะเปิดใช้งานโดยปริยายเมื่อ TextScaled = true
--textLabel.TextWrapped = true
spellTheText()
task.wait(1)
end

TextXAlignment
อ่านพร้อมๆ กัน
ความสามารถ: UI
TextLabel.TextXAlignment:Enum.TextXAlignment
ตัวอย่างโค้ด
การจัดแนข้อความ
-- วางสิ่งนี้ใน LocalScript ภายใน TextLabel/TextButton/TextBox
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
-- วนซ้ำผ่านรายการ Enum ของ 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 เป็นส่วนหนึ่งของเครื่องหมายการค้าที่จดทะเบียน และไม่ได้จดทะเบียนของเราในสหรัฐฯ และประเทศอื่นๆ