概要
属性
代码示例
游戏状态文本
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 参考
属性
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
endFontSize
Text
代码示例
渐隐横幅
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
endTextBounds
代码示例
动态文本框大小
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
代码示例
倒计时文本
-- 将此代码放置在 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)
endTextSize
代码示例
"轰轰!" 文本
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)
endTextStrokeTransparency
代码示例
文本高亮振荡
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()
endTextWrap
TextWrapped
代码示例
长文本换行
local textLabel = script.Parent
-- 此文本换行演示最适合在 200x50 px 的矩形上显示
textLabel.Size = UDim2.new(0, 200, 0, 50)
-- 一些要拼写的内容
local content = "这里是一串很长的单词,"
.. "最终会超过 UI 元素的宽度 "
.. "并形成换行。这对于真的很长的段落很有用。"
-- 一个每次拼写两个字符的函数
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)
endTextXAlignment
代码示例
文本对齐
-- 将此粘贴到 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