TextService

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建
服务
未复制

文本服务是内部负责处理游戏中文本显示的服务。

该类有两个成员函数:

TextService:GetTextSize() 函数给开发人员带来了计算特定文本字符串所需空间的能力,返回了一个 Vector2 像素大小。

TextService:FilterStringAsync() 函数必须用于正确过滤用户指定的文本(例如聊天消息或其他输入),以保护用户安全。不使用 Roblox 默认 Chat 或允许用户以其他方式输入文本的开发人员必须使用此函数。

概要

方法

属性

方法

GetTextSize

计算将在使用指定的格式参数和尺寸限制时与文本一起使用的 Vector2 维度(以像素计)。

注意,字体大小参数不会接受 Enum.FontSize 枚列。相反,应使用与 Enum.FontSize Enum 相应的整数大小。这不等于 Enum.FontSize Enum 的值。例如,对于 大小11 字体,整数 11 应使用。

此函数是 TextLabel.TextBounds 对象的 TextLabelTextButton 属性的有用替代。使用 TextLabel.TextBounds 属性计算所需的尺寸文本通常不便实用,因为需要创建一个 TextLabel 对象。

使用 GetTextSize,特定文本字符串在特定 TextLabelTextButton 中需要的尺寸可以在创建任何对象或设置文本属性之前计算。

建议开发人员向结果添加一像素的 padding,以确保没有文本被切关闭。

此方法仅限于列出在 Enum.Font 中的字体。要访问更多字体,您可以使用 TextService:GetTextBoundsAsync() 代替。

参数

string: string

文本大小要计算的字符串。

默认值:""
fontSize: number

用于表示字体大小的整数。

默认值:""
font: Enum.Font

使用的字体。

默认值:""
frameSize: Vector2

要使用的文本对象的 TextLabel.AbsoluteSize。需要计算文本如何包装。

默认值:""

返回

由指定格式的字符串所需的空间大小,以像素计算。

代码示例

This example showcases a possible usage of the GetTextSize function.

It computes the possible Vector2 dimensions (in pixels) of the string "Hello World" when the font size is 12, the font size SourceSans and the frame size is (1,1).

The expected return of this function is the Vector2 value 9, 13.

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

暂停

聊天翻译在遗产聊天中不支持。此方法已不再支持,不应使用。所有调用都返回一个空对象。翻译聊天消息只能通过 TextChatService 进行。

参数

stringToFilter: string
默认值:""
fromUserId: number
默认值:""
targetLocales: Array
默认值:""
默认值:"PrivateChat"

返回

FilterStringAsync

暂停

FilterStringAsync 函数会使用 TextService 对从用户那里收到的字符串进行过滤,并返回一个 TextFilterResult 可用于分配正确过滤的文本的返回值。

使用

该方法应在用户每次提交消信息时调用一次。不要缓存此函数的结果并重复使用它们用于分离的消息。如果用户多次提交相同的文本,每次发送消息时必须再次调用此方法。如果结果被缓存并重复使用,那么垃圾邮件检测和多种上下文过滤将被破坏,可能会危害用户安全。不正确使用缓存结果的游戏可能会面临审核。

然而,建议保留这些结果对象以向稍后加入服务器的用户显示相同的消息。例如:这可以用来安全有效地实现服务器聊天日志,始终使用最少限制的过滤对加入较晚的用户或显示像宠物名称这样的文本给加入游戏后的用户进行过滤,而不是在宠物首次生成和名称过滤之后。

可选的 Enum.TextFilterContext 参数不会影响查询的筛选结果。这个值将用于改进 Roblox 的文本筛选。

私人文本是只被特定玩家看到的内容,而不是每个玩家。例如,如果聊天被单个玩家或被选定的玩家群看到,那么聊天被认为是私人的。团队聊天或可能暴露给更广泛群组的聊天,例如服务器,被视为公开共的。如果您不确定您的文本符合什么标准,请留空可选字段。

注意:

  • 这个方法总是会产生一个文本筛选服务调用
  • 此方法可能会抛出,如果存在无法解决的服务错误。如果此函数抛出错误,请不要重试请求;该方法内部实现了自己的重试逻辑。如果该方法失败,不要向任何用户显示文本。
  • 该方法目前会抛出,如果 从用户ID 不在当前服务器上线。我们计划在未来支持离线或在不同服务器上的用户。

参数

stringToFilter: string

要被过滤的文本。

默认值:""
fromUserId: number

过滤文本的玩家的 id。

默认值:""

过滤消息将被用于的上下文。

默认值:"PrivateChat"

返回

代码示例

This code sample includes a simple demonstration of how TextService:FilterStringAsync() should be implemented in a system where players are allowed to name their pets.

Note, this code sample includes the server-side implementation only (to be placed in a Script). Examples of how to implement the client-side of this system are included under the RemoteEvent and RemoteFunction examples.

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

暂停

返回包含字体家族名称和脸孔的表。

返回的表结构如下:


type FaceInfo = {
Name: string, -- 例子:“常规”、“书”、“意大利体”、“薄意大利体”
Weight: Enum.FontWeight,
Style: Enum.FontStyle, -- 正常或斜体
}
type FamilyInfo = {
Name: string, -- 例子:“源 sans Pro”,“Grenze Gotisch”
Faces: {FaceInfo} -- 总是至少有 1 个,但最多可能有 18 个。
}

如果字体家族已由以前的调用 GetFamilyInfoAsyncContentProvider:PreloadAsync() 或具有 TextLabel.FontFace 属性设置的文本对象加载过,那么方法将无需放弃返回。

错误

该方法可能因网络错误而失败。你总是应该将其包装在 pcall 中进行错误处理。

在这些情况下投掷错误:

  • 传递的家庭是一个空字符串。
  • 下载家庭失败。
  • 资产ID无效或指向不存在的资产。

参数

assetId: ContentId

要查找的字体家族的资产 ID。

默认值:""

返回

关于字体家族的信息。

代码示例

This example showcases a possible usage of the TextService:GetFamilyInfoAsync() method.

It prints out information about a font family to the output window.

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

暂停

该方法与 TextService:GetTextSize() 相似,但使用 Font 对象而不是 Enum.Font ,其具有更多字体访问权限。

用于测量一些文本的大小将被给予一组属性,例如字符串、大小和字体。

这是一个产出函数,因为一些字体可能需要加载才能进行测量。如果字体已加载,那么它不会产生。ContentProvider:PreloadAsync() 可用于确保字体加载。

错误

该方法可能因网络错误而失败。你总是应该将其包装在 pcall 中进行错误处理。

在这些情况下投掷错误:

参数

GetTextBoundsParams 对象的参考。

默认值:""

返回

文本的大小为 Vector2

代码示例

This example shows how you can use TextService:GetTextBoundsAsync().

It measures the size of some text in a specific font, then prints the result.

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

暂停

参数

fontSize: number
默认值:""
font: Enum.Font
默认值:""

返回

活动