概要
方法
计算 Vector2 尺寸(以像素计),该使用指定的格式参数和尺寸限制时将被文本占用。
- FilterAndTranslateStringAsync(stringToFilter : string,fromUserId : number,targetLocales : Array,textContext : Enum.TextFilterContext):TextFilterTranslatedResult
聊天翻译不支持在传统聊天中使用。此方法已不支持,不应该使用。
- FilterStringAsync(stringToFilter : string,fromUserId : number,textContext : Enum.TextFilterContext):TextFilterResult
过滤从用户那里收到的字符串,并且返回一个 TextFilterResult 可以用来按照正确过滤的文本进行分配。
返回一个包含字体家族名称和面部的表。
计算给予参数的宽度和高度。
属性
方法
GetTextSize
计算 Vector2 尺寸(以像素计),该使用指定的格式参数和尺寸限制时将被文本占用。
注意,字体大小参数不会接受 Enum.FontSize 枚。 相反,与 Enum.FontSize 枚相对应的整数大小应该使用。 这不是等于 Enum.FontSize 枚的值。 例如,对于 2> Size122> 字体,整体大小应该使用 5>155> 。
此函数是 TextLabel.TextBounds 和 TextLabel 对象的有用替代品,因为它使用 TextButton 属性来计算尺寸文本通常需要很不方便,因为它需要一个 2>Class.TextLabel2> 对象来创建。
使用 GetTextSize,在特定 TextLabel 或 TextButton 中的特定文本字符的尺寸可以在任何对象创建或设置文本属性之前计算。
开发人员建议添加结果中的一个像素垫子以确保不要切关闭任何文本。
此方法仅限于列在 Enum.Font 中列出的字体。要获得更多字体的访问权限,您可以使用 TextService:GetTextBoundsAsync() 代替。
参数
返回
需要的空间的大小,以像素计量,通过指定的格式化字符串。
代码示例
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.
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 使用。
参数
返回
FilterStringAsync
FilterStringAsync 函数使用 TextService 过滤收到的用户字符串,并返回一个 TextFilterResult ,可以根据正确过滤的文本进行分配。
使用
此方法每次用户提交消信息时调用一次。不要保存此函数的结果,并且在发送消息时再次使用它。如果用户多次提交同一文本,此方法必须调用一次,直到发送消息为止。如果结果被储存并重用,垃圾邮件检测和许多形式的上下文敏感过滤器将会被破坏,可能会导致用户的安全风险。如果
但是,建议您保留这些结果对象以便在后续加入服务器的用户显示相同的消息。例如:这可以用于安全和有效地实现用户加入服务器后使用最小限制过滤器对用户使用的聊天日志,或用于显示文本,例如宠物名称,在宠物生成后的第一个名称过滤器。
可选的 Enum.TextFilterContext 参数不会影响查询的过滤结果。此值将用于改进 Roblox 的文本过滤器。
私人文本是只有特定玩家看到的内容,而不是每个玩家。例如,如果聊天被单个玩家或选定的一群玩家看到,聊天就会被视为私人。 对于群组队或可能被更广泛公开的聊天,如服务器,聊天为私人。 如果您不确定您的文本是否适用,请留下选定的输入框为空。
注意:
- 这个方法总是会返回以便调用文本过滤服务
- 此方法可能会发生错误,如果无法解决服务错误。 如果此函数发生错误,请不要重试请求;此方法内置了自己的重试逻辑。 如果此方法失败,请不要显示文本给任何用户。
- 此方法目前会抛出,如果 从 UserId 不在当前服务器上。我们打算在未来支持离线或在不同服务器上的用户。
参数
要过滤的文本。
玩家过滤器的用户ID。
过滤器将使用的上下文。
返回
代码示例
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.
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, -- 普通或 italic}type FamilyInfo = {Name: string, -- 例子:“源 Sans Pro”、“Grenze Gotisch”Faces: {FaceInfo} -- 总是至少有 1 个,但也可以有 18 个。}
如果字体家族已经通过上一个调用到 GetFamilyInfoAsync , ContentProvider:PreloadAsync() 或具有 TextLabel.FontFace 属性的文本对设置,那么方法将无法返回,而不会提供输出。
错误
此方法因网络错误而失败。您应该总是包围它在 pcall 为错误处理。
在这些场景中投掷一个错误:
- 通过的家庭是一个空的字符串。
- 下载家族失败。
- 资产 ID 无效或指向不存在的资产。
参数
字体家族的资产ID。
返回
关于字体家族的信息。
代码示例
This example showcases a possible usage of the TextService:GetFamilyInfoAsync() method.
It prints out information about a font family to the output window.
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,这可以访问更多字体。
用于测量一些文本的大小,例如给予 string 、大小和字符串体等属性。
这是一个输出函数,因为有些字体可能需要加载才能测量它们。如果字体已加载,则不会输出。ContentProvider:PreloadAsync() 可以用于确保字体已加载。
错误
此方法因网络错误而失败。您应该总是包围它在 pcall 为错误处理。
在这些场景中投掷一个错误:
- Class.GetTextBoundsParams.Font 有一个空的家庭。
- paramètres 参数为空。
- 字体家族或字体面无法下载。
参数
一个引用 GetTextBoundsParams 对象的引用。
返回
代码示例
This example shows how you can use TextService:GetTextBoundsAsync().
It measures the size of some text in a specific font, then prints the result.
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)