概要
方法
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