Clase de motor
TextLabel
*Este contenido se traduce usando la IA (Beta) y puede contener errores. Para ver esta página en inglés, haz clic en aquí.
Resumen
Propiedades
Muestras de código
Texto del Estado del Juego
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Coloca un StringValue llamado "GameState" en el ReplicatedStorage
local vGameState = ReplicatedStorage:WaitForChild("GameState")
-- Coloca este código en un TextLabel
local textLabel = script.Parent
-- Algunos colores que usaremos con TextColor3
local colorNormal = Color3.new(0, 0, 0) -- negro
local colorCountdown = Color3.new(1, 0.5, 0) -- naranja
local colorRound = Color3.new(0.25, 0.25, 1) -- azul
-- Ejecutaremos esta función para actualizar el TextLabel a medida que el estado de la
-- juego cambia.
local function update()
-- Actualiza el texto
textLabel.Text = "Estado: " .. vGameState.Value
-- Establece el color del texto según el estado actual del juego
if vGameState.Value == "Countdown" then
textLabel.TextColor3 = colorCountdown
elseif vGameState.Value == "Round" then
textLabel.TextColor3 = colorRound
else
textLabel.TextColor3 = colorNormal
end
end
-- Patrón: actualizar una vez cuando comenzamos y también cuando cambia vGameState
-- Siempre deberíamos ver el GameState más actualizado.
update()
vGameState.Changed:Connect(update)Referencia API
Propiedades
Font
Muestras de código
Mostrar todas las fuentes
local frame = script.Parent
-- Crear un TextLabel mostrando cada fuente
for _, font in pairs(Enum.Font:GetEnumItems()) do
local textLabel = Instance.new("TextLabel")
textLabel.Name = font.Name
-- Establecer las propiedades del texto
textLabel.Text = font.Name
textLabel.Font = font
-- Algunas propiedades de renderizado
textLabel.TextSize = 24
textLabel.TextXAlignment = Enum.TextXAlignment.Left
-- Dimensionar el marco igual a la altura del texto
textLabel.Size = UDim2.new(1, 0, 0, textLabel.TextSize)
-- Agregar al marco padre
textLabel.Parent = frame
end
-- Organizar los marcos en una lista (si no lo están ya)
if not frame:FindFirstChildOfClass("UIListLayout") then
local uiListLayout = Instance.new("UIListLayout")
uiListLayout.Parent = frame
endFuente de Ciclo
local textLabel = script.Parent
while true do
-- Iterar sobre todas las diferentes fuentes
for _, font in pairs(Enum.Font:GetEnumItems()) do
textLabel.Font = font
textLabel.Text = font.Name
task.wait(1)
end
endFontSize
Text
Muestras de código
Banner Desvanecido
local TweenService = game:GetService("TweenService")
local textLabel = script.Parent
local content = {
"¡Bienvenido a mi juego!",
"¡Asegúrate de divertirte!",
"¡Por favor, haz sugerencias!",
"¡Sé amable con otros jugadores!",
"¡No molestes a otros jugadores!",
"¡Echa un vistazo a la tienda!",
"Consejo: ¡No mueras!",
}
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
-- Paso 0: Desvanecer antes de hacer cualquier cosa
fadeOut:Play()
task.wait(tweenInfo.Time)
-- Paso 1: elegir contenido que no fue el último mostrado
local index
repeat
index = RNG:NextInteger(1, #content)
until lastIndex ~= index
-- Asegúrate de no mostrar lo mismo la próxima vez
lastIndex = index
-- Paso 2: mostrar el contenido
textLabel.Text = content[index]
fadeIn:Play()
task.wait(tweenInfo.Time + 1)
endEmoji en Texto
local textLabel = script.Parent
local moods = {
["happy"] = "😃",
["sad"] = "😢",
["neutral"] = "😐",
["tired"] = "😫",
}
while true do
for mood, face in pairs(moods) do
textLabel.Text = "¡Me siento " .. mood .. "! " .. face
task.wait(1)
end
endTextBounds
Muestras de código
Tamaño dinámico de TextBox
local textBox = script.Parent
-- El tamaño más pequeño al que podrá llegar el TextBox
local minWidth, minHeight = 10, 10
-- Establecer la alineación para que nuestro texto no se mueva un poco mientras escribimos
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
Muestras de código
Texto de Cuenta Regresiva
-- Coloca este código en un LocalScript dentro de un TextLabel/TextButton
local textLabel = script.Parent
-- Algunos colores que usaremos con TextColor3
local colorNormal = Color3.new(0, 0, 0) -- negro
local colorSoon = Color3.new(1, 0.5, 0.5) -- rojo
local colorDone = Color3.new(0.5, 1, 0.5) -- verde
-- Bucle infinito
while true do
-- Cuenta hacia atrás desde 10 hasta 1
for i = 10, 1, -1 do
-- Establece el texto
textLabel.Text = "Tiempo: " .. i
-- Establece el color basado en cuánto tiempo queda
if i > 3 then
textLabel.TextColor3 = colorNormal
else
textLabel.TextColor3 = colorSoon
end
task.wait(1)
end
textLabel.Text = "¡VAMOS!"
textLabel.TextColor3 = colorDone
task.wait(2)
endTextSize
Muestras de código
"¡Kaboom!" Texto
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
Muestras de código
Oscilación de Resalte de Texto
local textLabel = script.Parent
-- Qué tan rápido debe parpadear el resalte
local freq = 2
-- Establecer el color de resalte a amarillo
textLabel.TextStrokeColor3 = Color3.new(1, 1, 0)
while true do
-- math.sin oscila de -1 a 1, así que cambiamos el rango a 0 a 1:
local transparencia = math.sin(workspace.DistributedGameTime * math.pi * freq) * 0.5 + 0.5
textLabel.TextStrokeTransparency = transparencia
task.wait()
endTextWrap
TextWrapped
Muestras de código
Ajuste de Texto Largo
local textLabel = script.Parent
-- Esta demostración de ajuste de texto se muestra mejor en un rectángulo de 200x50 px
textLabel.Size = UDim2.new(0, 200, 0, 50)
-- Algunos contenidos para desplegar
local content = "Aquí hay una larga cadena de palabras que "
.. "eventualmente excederá el ancho del elemento UI "
.. "y formará saltos de línea. Útil para párrafos "
.. "que son realmente largos."
-- Una función que desplegará el texto dos caracteres a la vez
local function spellTheText()
-- Iterar de 1 a la longitud de nuestro contenido
for i = 1, content:len() do
-- Obtener un substring de nuestro contenido: 1 a i
textLabel.Text = content:sub(1, i)
-- Colorear el texto si no cabe en nuestra caja
if textLabel.TextFits then
textLabel.TextColor3 = Color3.new(0, 0, 0) -- Negro
else
textLabel.TextColor3 = Color3.new(1, 0, 0) -- Rojo
end
-- Esperar un breve momento en longitudes pares
if i % 2 == 0 then
task.wait()
end
end
end
while true do
-- Desplegar el texto con escala/envuelto desactivado
textLabel.TextWrapped = false
textLabel.TextScaled = false
spellTheText()
task.wait(1)
-- Desplegar el texto con ajuste activado
textLabel.TextWrapped = true
textLabel.TextScaled = false
spellTheText()
task.wait(1)
-- Desplegar el texto con la escala de texto activada
-- Nota: El texto se torna rojo (TextFits = false) una vez que el texto debe ser
-- reducido para ajustarse dentro del elemento UI.
textLabel.TextScaled = true
-- Nota: TextWrapped se habilita implícitamente cuando TextScaled = true
--textLabel.TextWrapped = true
spellTheText()
task.wait(1)
endTextXAlignment
Muestras de código
Alineación de texto
-- Pega esto en un LocalScript dentro de un 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
-- Iterar sobre ambos elementos del enum TextXAlignment y 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