Lớp công cụ
TextLabel
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Tóm Tắt
Thuộc Tính
Mẫu mã
Văn bản Trạng thái Trò chơi
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Đặt một StringValue có tên "GameState" trong ReplicatedStorage
local vGameState = ReplicatedStorage:WaitForChild("GameState")
-- Đặt mã này trong một TextLabel
local textLabel = script.Parent
-- Một số màu sắc mà chúng tôi sẽ sử dụng với TextColor3
local colorNormal = Color3.new(0, 0, 0) -- đen
local colorCountdown = Color3.new(1, 0.5, 0) -- cam
local colorRound = Color3.new(0.25, 0.25, 1) -- xanh
-- Chúng ta sẽ chạy chức năng này để cập nhật TextLabel khi trạng thái của
-- trò chơi thay đổi.
local function update()
-- Cập nhật văn bản
textLabel.Text = "Trạng thái: " .. vGameState.Value
-- Thiết lập màu sắc của văn bản dựa trên trạng thái trò chơi hiện tại
if vGameState.Value == "Countdown" then
textLabel.TextColor3 = colorCountdown
elseif vGameState.Value == "Round" then
textLabel.TextColor3 = colorRound
else
textLabel.TextColor3 = colorNormal
end
end
-- Mẫu: cập nhật một lần khi chúng ta bắt đầu và cũng khi vGameState thay đổi
-- Chúng ta luôn nên thấy GameState được cập nhật nhất.
update()
vGameState.Changed:Connect(update)Tài Liệu Tham Khảo API
Thuộc Tính
Font
Mẫu mã
Hiển Thị Tất Cả Phông Chữ
local frame = script.Parent
-- Tạo một TextLabel hiển thị mỗi phông chữ
for _, font in pairs(Enum.Font:GetEnumItems()) do
local textLabel = Instance.new("TextLabel")
textLabel.Name = font.Name
-- Đặt thuộc tính văn bản
textLabel.Text = font.Name
textLabel.Font = font
-- Một số thuộc tính hiển thị
textLabel.TextSize = 24
textLabel.TextXAlignment = Enum.TextXAlignment.Left
-- Đặt kích thước của khung bằng chiều cao của văn bản
textLabel.Size = UDim2.new(1, 0, 0, textLabel.TextSize)
-- Thêm vào khung cha
textLabel.Parent = frame
end
-- Sắp xếp các khung thành một danh sách (nếu chúng chưa có sẵn)
if not frame:FindFirstChildOfClass("UIListLayout") then
local uiListLayout = Instance.new("UIListLayout")
uiListLayout.Parent = frame
endPhông Chu Kỳ
local textLabel = script.Parent
while true do
-- Lặp qua tất cả các phông chữ khác nhau
for _, font in pairs(Enum.Font:GetEnumItems()) do
textLabel.Font = font
textLabel.Text = font.Name
task.wait(1)
end
endFontSize
Text
Mẫu mã
Bảng Hiện Ngang Mờ
local TweenService = game:GetService("TweenService")
local textLabel = script.Parent
local content = {
"Chào mừng bạn đến với trò chơi của tôi!",
"Hãy chắc chắn rằng bạn vui vẻ!",
"Xin vui lòng đưa ra đề xuất!",
"Hãy thân thiện với các người chơi khác!",
"Đừng quấy rối các người chơi khác!",
"Hãy kiểm tra tiệm!",
"Mẹo: Đừng chết!",
}
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
-- Bước 0: Làm mờ trước khi làm bất cứ điều gì
fadeOut:Play()
task.wait(tweenInfo.Time)
-- Bước 1: chọn nội dung mà không phải là cái đã hiển thị lần trước
local index
repeat
index = RNG:NextInteger(1, #content)
until lastIndex ~= index
-- Đảm bảo chúng ta không hiển thị cùng một thứ lần sau
lastIndex = index
-- Bước 2: hiển thị nội dung
textLabel.Text = content[index]
fadeIn:Play()
task.wait(tweenInfo.Time + 1)
endBiểu tượng cảm xúc trong văn bản
local textLabel = script.Parent
local moods = {
["happy"] = "😃",
["sad"] = "😢",
["neutral"] = "😐",
["tired"] = "😫",
}
while true do
for mood, face in pairs(moods) do
textLabel.Text = "Tôi cảm thấy " .. mood .. "! " .. face
task.wait(1)
end
endTextBounds
Mẫu mã
Kích Thước TextBox Động
local textBox = script.Parent
-- Kích thước nhỏ nhất mà TextBox có thể có
local minWidth, minHeight = 10, 10
-- Đặt căn chỉnh để văn bản của chúng ta không bị rung trong khi gõ
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
Mẫu mã
Văn bản đếm ngược
-- Đặt mã này trong một LocalScript bên trong một TextLabel/TextButton
local textLabel = script.Parent
-- Một số màu chúng ta sẽ sử dụng với TextColor3
local colorNormal = Color3.new(0, 0, 0) -- đen
local colorSoon = Color3.new(1, 0.5, 0.5) -- đỏ
local colorDone = Color3.new(0.5, 1, 0.5) -- xanh
-- Lặp vô hạn
while true do
-- Đếm ngược từ 10 đến 1
for i = 10, 1, -1 do
-- Đặt văn bản
textLabel.Text = "Thời gian: " .. i
-- Đặt màu dựa trên thời gian còn lại
if i > 3 then
textLabel.TextColor3 = colorNormal
else
textLabel.TextColor3 = colorSoon
end
task.wait(1)
end
textLabel.Text = "Đi!"
textLabel.TextColor3 = colorDone
task.wait(2)
endTextSize
Mẫu mã
"Kaboom!" Văn bản
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)
endTextStrokeTransparency
Mẫu mã
Sự dao động của độ sáng văn bản
local textLabel = script.Parent
-- Tốc độ nhấp nháy của độ sáng
local freq = 2
-- Đặt màu vàng cho độ sáng
textLabel.TextStrokeColor3 = Color3.new(1, 1, 0)
while true do
-- math.sin dao động từ -1 đến 1, vì vậy chúng ta thay đổi phạm vi thành 0 đến 1:
local transparency = math.sin(workspace.DistributedGameTime * math.pi * freq) * 0.5 + 0.5
textLabel.TextStrokeTransparency = transparency
task.wait()
endTextWrap
TextWrapped
Mẫu mã
Qu wrapping văn bản dài
local textLabel = script.Parent
-- Mẫu demo wrapping văn bản này tốt nhất được hiển thị trên một hình chữ nhật 200x50 px
textLabel.Size = UDim2.new(0, 200, 0, 50)
-- Một số nội dung để đánh vần
local content = "Đây là một chuỗi từ dài mà sẽ "
.. "cuối cùng vượt quá chiều rộng của phần tử UI "
.. "và tạo ra các dòng ngắt. Hữu ích cho các đoạn văn "
.. "rất dài."
-- Một hàm sẽ đánh vần văn bản hai ký tự một lần
local function spellTheText()
-- Lặp từ 1 đến chiều dài của nội dung
for i = 1, content:len() do
-- Lấy một chuỗi con của nội dung: từ 1 đến i
textLabel.Text = content:sub(1, i)
-- Đổi màu văn bản nếu nó không vừa trong hộp của chúng ta
if textLabel.TextFits then
textLabel.TextColor3 = Color3.new(0, 0, 0) -- Đen
else
textLabel.TextColor3 = Color3.new(1, 0, 0) -- Đỏ
end
-- Chờ một chút vào các chiều dài chẵn
if i % 2 == 0 then
task.wait()
end
end
end
while true do
-- Đánh vần văn bản với scale/wrap tắt
textLabel.TextWrapped = false
textLabel.TextScaled = false
spellTheText()
task.wait(1)
-- Đánh vần văn bản với wrap bật
textLabel.TextWrapped = true
textLabel.TextScaled = false
spellTheText()
task.wait(1)
-- Đánh vần văn bản với tỷ lệ văn bản bật
-- Lưu ý: Văn bản chuyển sang màu đỏ (TextFits = false) khi văn bản phải
-- được thu nhỏ để vừa với phần tử UI.
textLabel.TextScaled = true
-- Lưu ý: TextWrapped được bật ngầm khi TextScaled = true
--textLabel.TextWrapped = true
spellTheText()
task.wait(1)
endTextXAlignment
Mẫu mã
Căn chỉnh văn bản
-- Dán đoạn mã này vào một LocalScript trong một 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
-- Lặp qua cả hai mục enum TextXAlignment và 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