概要
方法
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 |
API 參考
方法
FilterAndTranslateStringAsync
FilterStringAsync
TextService:FilterStringAsync(
參數
| 預設值:"PrivateChat" |
範例程式碼
寵物名稱過濾範例
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
-- 產生一個新的執行緒以避免阻塞更新
task.spawn(function()
-- 獲取此用戶的過濾字串
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 以捕捉錯誤
local success, result = pcall(function()
return TextService:FilterStringAsync(petName, fromUserId)
end)
if success then
filterResults[fromUserId] = result
broadcastPetName(fromUserId)
else
print("無法過濾寵物名稱")
end
end)
RequestAllPetNames.OnServerInvoke = function(player)
local toUserId = player.UserId
local petNames = {}
-- 遍歷過濾 結果並過濾將要發送的寵物名稱
for fromUserId, filterResult in pairs(filterResults) do
local filteredString = filterResult:GetNonChatStringForUserAsync(toUserId)
filteredString = filteredString or ""
-- 需要將 userId 轉換為 字串,以便不能通過遠端函數發送
petNames[tostring(fromUserId)] = filteredString
end
return petNames
end
Players.PlayerRemoving:Connect(function(oldPlayer)
local userId = oldPlayer.UserId
filterResults[userId] = nil
end)GetFamilyInfoAsync
參數
assetId:ContentId |
範例程式碼
獲取字體家族資訊
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("字體名稱:", info.Name)
print("字體樣式:")
for _, face in info.Faces do
print("--------")
print("名稱:", face.Name)
print("粗細:", face.Weight.Name)
print("風格:", face.Style.Name)
end
endGetTextBoundsAsync
參數
返回
範例程式碼
測量文字大小
local TextService = game:GetService("TextService")
-- 宣告參數
local params = Instance.new("GetTextBoundsParams")
params.Text = "你好,世界!"
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
返回
範例程式碼
獲取文本大小
local TextService = game:GetService("TextService")
local function getTextBounds()
local message = "你好,世界!"
local size = Vector2.new(1, 1)
local bounds = TextService:GetTextSize(message, 12, "SourceSans", size)
return bounds
end
print(getTextBounds())GetTextSizeOffsetAsync
返回
範例程式碼
獲取文本大小偏移
local TextService = game:GetService("TextService")
local label = script.Parent.TextLabel
-- 根據玩家首選的文本大小設置獲取調整後的偏移
local success, offset = pcall(function()
return TextService:GetTextSizeOffsetAsync(label.TextSize, label.FontFace)
end)
if success then
-- 通過添加偏移來調整標籤的核心高度
label.Size = UDim2.fromOffset(label.Size.X.Offset, label.Size.Y.Offset + offset)
end