TextService

Mostrar obsoleto

*Este contenido se traduce usando la IA (Beta) y puede contener errores. Para ver esta página en inglés, haz clic en aquí.

No creable
Servicio
No replicado

El TextService es un servicio responsable internamente de manejar la visualización de texto en el juego.

Esta clase tiene dos funciones de miembro:

La función TextService:GetTextSize() da a los desarrolladores la capacidad de calcular el espacio requerido para una cadena de texto específica con formato especificado, devolviendo un tamaño de píxel de Vector2 .

La función TextService:FilterStringAsync() es requerida para filtrar adecuadamente el texto especificado por el usuario (como mensajes de chat o otras entradas) con el interés de la seguridad del usuario.Los desarrolladores que no usan el predeterminado de Roblox Chat o permiten que los usuarios ingresen texto de otra manera deben usar esta función.

Resumen

Métodos

Propiedades

Métodos

GetTextSize

Parámetros

string: string
Valor predeterminado: ""
fontSize: number
Valor predeterminado: ""
font: Enum.Font
Valor predeterminado: ""
frameSize: Vector2
Valor predeterminado: ""

Devuelve

Muestras de código

TextService: Getting the Text Size

local TextService = game:GetService("TextService")
local function getTextBounds()
local message = "Hello World"
local size = Vector2.new(1, 1)
local bounds = TextService:GetTextSize(message, 12, "SourceSans", size)
return bounds + Vector2.new(1, 1)
end
print(getTextBounds())

FilterAndTranslateStringAsync

Proporciona

Parámetros

stringToFilter: string
Valor predeterminado: ""
fromUserId: number
Valor predeterminado: ""
targetLocales: Array
Valor predeterminado: ""
Valor predeterminado: "PrivateChat"

Devuelve

FilterStringAsync

Proporciona

Parámetros

stringToFilter: string
Valor predeterminado: ""
fromUserId: number
Valor predeterminado: ""
Valor predeterminado: "PrivateChat"

Devuelve

Muestras de código

Pet Name Filter Example

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TextService = game:GetService("TextService")
local Players = game:GetService("Players")
local Remotes = Instance.new("Folder")
Remotes.Name = "PetNamingRemotes"
Remotes.Parent = ReplicatedStorage
local UserNamedPet = Instance.new("RemoteEvent")
UserNamedPet.Name = "UserNamedPet"
UserNamedPet.Parent = Remotes
local SendPetName = Instance.new("RemoteEvent")
SendPetName.Name = "SendPetName"
SendPetName.Parent = Remotes
local RequestAllPetNames = Instance.new("RemoteFunction")
RequestAllPetNames.Name = "RequestAllPetNames"
RequestAllPetNames.Parent = Remotes
local filterResults = {}
local function broadcastPetName(userId)
local filterResult = filterResults[userId]
if filterResult then
for _, player in pairs(Players:GetPlayers()) do
if player then
-- spawn a new thread so as to not yield the update
task.spawn(function()
-- grab the filtered string for this user
local toUserId = player.UserId
local filteredString = filterResult:GetNonChatStringForUserAsync(toUserId)
filteredString = filteredString or ""
SendPetName:FireClient(player, userId, filteredString)
end)
end
end
end
end
UserNamedPet.OnServerEvent:Connect(function(player, petName)
local fromUserId = player.UserId
-- pcall to catch errors
local success, result = pcall(function()
return TextService:FilterStringAsync(petName, fromUserId)
end)
if success then
filterResults[fromUserId] = result
broadcastPetName(fromUserId)
else
print("Could not filter pet name")
end
end)
RequestAllPetNames.OnServerInvoke = function(player)
local toUserId = player.UserId
local petNames = {}
-- go through filter results and filter the pet name to be sent
for fromUserId, filterResult in pairs(filterResults) do
local filteredString = filterResult:GetNonChatStringForUserAsync(toUserId)
filteredString = filteredString or ""
-- need to convert userId to string so it can't be sent via a remote function
petNames[tostring(fromUserId)] = filteredString
end
return petNames
end
Players.PlayerRemoving:Connect(function(oldPlayer)
local userId = oldPlayer.UserId
filterResults[userId] = nil
end)

GetFamilyInfoAsync

Proporciona

Parámetros

assetId: ContentId
Valor predeterminado: ""

Devuelve

Muestras de código

TextService: Getting font family information

local TextService = game:GetService("TextService")
local familyToCheck = "rbxasset://fonts/families/Arial.json"
-- This is a yield function which may take up to a few seconds to download the font.
local info = TextService:GetFamilyInfoAsync(familyToCheck)
print("Name of the font:", info.Name)
print("Faces:")
for _, face in info.Faces do
print("--------")
print("Name:", face.Name)
print("Weight:", face.Weight.Name)
print("Style:", face.Style.Name)
end

GetTextBoundsAsync

Proporciona

Parámetros

Valor predeterminado: ""

Devuelve

Muestras de código

TextService: Measuring text size

local TextService = game:GetService("TextService")
local params = Instance.new("GetTextBoundsParams")
params.Text = "hello world!"
params.Font = Font.new("rbxasset://fonts/families/GrenzeGotisch.json", Enum.FontWeight.Thin)
params.Size = 20
params.Width = 200
local size = TextService:GetTextBoundsAsync(params)
print("The size of the text is:", size)

GetTextSizeOffsetAsync

Proporciona

Parámetros

fontSize: number
Valor predeterminado: ""
font: Enum.Font
Valor predeterminado: ""

Devuelve

Eventos