Klasa silnika
TextService
*Ta zawartość została przetłumaczona przy użyciu narzędzi AI (w wersji beta) i może zawierać błędy. Aby wyświetlić tę stronę w języku angielskim, kliknij tutaj.
Podsumowanie
Metody
FilterAndTranslateStringAsync(stringToFilter: string,fromUserId: number,targetLocales: {any},textContext: Enum.TextFilterContext):TextFilterTranslatedResult |
FilterStringAsync(stringToFilter: string,fromUserId: number,textContext: Enum.TextFilterContext):TextFilterResult |
GetFamilyInfoAsync(assetId: ContentId):Dictionary |
GetTextBoundsAsync(params: GetTextBoundsParams):Vector2 |
GetTextSizeOffsetAsync(fontSize: number,font: Font):number |
Materiały referencyjne dotyczące interfejsów API
Metody
FilterAndTranslateStringAsync
FilterStringAsync
TextService:FilterStringAsync(
Parametry
| Wartość domyślna: "PrivateChat" |
Zwroty
Przykłady kodu
Przykład Filtru Nazwy Pupila
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
-- uruchom nowy wątek, aby nie wstrzymywać aktualizacji
task.spawn(function()
-- pobierz przefiltrowany ciąg dla tego użytkownika
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, aby złapać błędy
local success, result = pcall(function()
return TextService:FilterStringAsync(petName, fromUserId)
end)
if success then
filterResults[fromUserId] = result
broadcastPetName(fromUserId)
else
print("Nie można przefiltrować nazwy pupila")
end
end)
RequestAllPetNames.OnServerInvoke = function(player)
local toUserId = player.UserId
local petNames = {}
-- przejrzyj wyniki filtrowania i przefiltruj nazwę pupila do wysłania
for fromUserId, filterResult in pairs(filterResults) do
local filteredString = filterResult:GetNonChatStringForUserAsync(toUserId)
filteredString = filteredString or ""
-- trzeba zamienić userId na ciąg, aby nie można było go wysłać przez funkcję zdalną
petNames[tostring(fromUserId)] = filteredString
end
return petNames
end
Players.PlayerRemoving:Connect(function(oldPlayer)
local userId = oldPlayer.UserId
filterResults[userId] = nil
end)GetFamilyInfoAsync
Parametry
assetId:ContentId |
Zwroty
Przykłady kodu
Uzyskaj informacje o rodzinie czcionek
local TextService = game:GetService("TextService")
local familyToCheck = "rbxasset://fonts/families/Arimo.json"
local success, info = pcall(function()
return TextService:GetFamilyInfoAsync(familyToCheck)
end)
if success then
print("Nazwa czcionki:", info.Name)
print("Style:")
for _, face in info.Faces do
print("--------")
print("Nazwa:", face.Name)
print("Waga:", face.Weight.Name)
print("Styl:", face.Style.Name)
end
endGetTextBoundsAsync
Parametry
Zwroty
Przykłady kodu
Mierzenie rozmiaru tekstu
local TextService = game:GetService("TextService")
-- Deklaruj parametry
local params = Instance.new("GetTextBoundsParams")
params.Text = "Witaj świecie!"
params.Font = Font.new("rbxasset://fonts/families/GrenzeGotisch.json", Enum.FontWeight.Thin)
params.Size = 20
params.Width = 200
local success, bounds = pcall(function()
return TextService:GetTextBoundsAsync(params)
end)
if success then
print(bounds)
endGetTextSize
Zwroty
Przykłady kodu
Uzyskiwanie rozmiaru tekstu
local TextService = game:GetService("TextService")
local function getTextBounds()
local message = "Witaj świecie!"
local size = Vector2.new(1, 1)
local bounds = TextService:GetTextSize(message, 12, "SourceSans", size)
return bounds
end
print(getTextBounds())GetTextSizeOffsetAsync
Zwroty
Przykłady kodu
Uzyskaj przesunięcie rozmiaru tekstu
local TextService = game:GetService("TextService")
local label = script.Parent.TextLabel
-- Uzyskaj przesunięcie dostosowane do preferencji rozmiaru tekstu gracza
local success, offset = pcall(function()
return TextService:GetTextSizeOffsetAsync(label.TextSize, label.FontFace)
end)
if success then
-- Dostosuj wysokość podstawową etykiety, dodając przesunięcie
label.Size = UDim2.fromOffset(label.Size.X.Offset, label.Size.Y.Offset + offset)
end