Skip to content

TextService ​

概览 ​

字段值
KindService · 服务 Service
Realmcommon

继承关系 ​

  • Unit(5 属性 / 28 函数 / 6 事件)
    • [TextService](5 函数)

> Service 继承 Unit 仅表示运行时类型关系;作者可用 API 以当前 Service 页面显式列出的成员为准,不自动包含 Unit 的创建、层级或销毁能力。

提供文本尺寸测量、字体族查询与玩家文本过滤能力。可通过它测量若干字体与字号下文本占据的宽高,也能按参数表获得文本边界,并在需要时对玩家输入内容做审核过滤。

适用场景 ​

常用于界面排版前预先计算文本尺寸、判断文字是否需要换行或缩放,以及在聊天、昵称、自定义输入等场景中过滤玩家提交的字符串。

使用要点 ​

先通过 game:GetService("TextService") 取得服务实例,然后调用 GetTextSize 测量文本宽高并可按需传入 Vector2.New(80, 0) 这类限宽参数观察换行效果;需要按参数表测量时改用 GetTextBoundsAsync,查询字号偏移使用 GetTextSizeOffsetAsync,获取字体族信息使用 GetFamilyInfoAsync,过滤玩家文本则调用 FilterStringAsync。

注意事项 ​

测量类接口依赖客户端文本渲染,需在客户端调用才能得到有效结果。过滤接口在审核服务不可用时会原样返回文本,因此不能依赖其返回值判断内容是否安全。字体参数可传入 Enums.TextFontType 下的枚举项。

代码示例 ​

测量文本尺寸、过滤文本并查询字体族信息 ​

lua
-- @runtime client
-- 客户端文本渲染才能给出有效测量结果,因此这里全部走客户端链路
local textService = game:GetService("TextService")
local taskService = game:GetService("Task")

-- 不限宽测量:frameSize 宽度传 0 表示不换行,返回文本自然占用的宽高
local plainSize = textService:GetTextSize("Hello TextService", 30, Enums.TextFontType.HKXZYT_W9_GB, Vector2.New(0, 0))
print(string.format("不限宽尺寸: %.2f x %.2f", plainSize.x, plainSize.y))

-- 换成粗圆体并限定 80 像素宽,高度会随换行行数增加,可据此决定文本框高度
local wrappedSize = textService:GetTextSize("Hello TextService", 30, Enums.TextFontType.YUANTI_BOLD, Vector2.New(80, 0))
print(string.format("限宽 80 尺寸: %.2f x %.2f", wrappedSize.x, wrappedSize.y))

-- 展示前先过一次内容审核;审核服务不可用时接口会原样返回文本,无需额外判空
local filtered = textService:FilterStringAsync("这是一段待展示的玩家文本")
print("过滤结果: " .. tostring(filtered))

-- 以 Async 结尾的接口必须放进协程,避免阻塞当前帧
if taskService ~= nil then
    taskService:Spawn(function()
        -- 按参数表测量:Font/Size/Width 组合等价于一次带换行约束的测量
        local bounds = textService:GetTextBoundsAsync({
            Text = "Hello TextService",
            Font = Enums.TextFontType.HKXZYT_W9_GB,
            Size = 30,
            Width = 200,
        })
        if bounds ~= nil then
            print(string.format("GetTextBoundsAsync 结果: %.2f x %.2f", bounds.x, bounds.y))
        end

        -- 字体族信息是 table 返回值,先判断类型再读取,避免索引到非结构化结果
        local familyInfo = textService:GetFamilyInfoAsync(Enums.TextFontType.HKXZYT_W9_GB)
        if type(familyInfo) == "table" then
            print("字体族信息已获取")
        else
            print("字体族信息不可用")
        end

        -- 玩家字号偏移用于对齐排版高度,当前无该设置时恒为 0
        local offset = textService:GetTextSizeOffsetAsync(30, Enums.TextFontType.HKXZYT_W9_GB)
        print("字号偏移: " .. tostring(offset))
    end)
end

关联类型 ​

函数 (5) ​

GetTextSize ​

签名:GetTextSize(text: String, fontSize: Int, font?: String, frameSize?: Vector2) -> Vector2 (文本宽高)

计算指定文本在给定字号下的显示尺寸,返回文本的宽高(Vector2)。可选传入字体与限制框尺寸,用于控制字体样式与换行宽度。

参数类型说明
textString文本
fontSizeInt字号
font?String字体,取 TextFontType 枚举值,省略用默认字体
frameSize?Vector2容器尺寸,仅宽度参与换行约束

返回值 Vector2 (文本宽高)

示例代码

用 GetTextSize 测量不限宽与限宽折行后的文本尺寸

lua
-- @runtime client
-- 获取文本服务实例:文本测量依赖客户端渲染
local textService = game:GetService("TextService")
local content = "Hello TextService"
-- 不限宽测量:frameSize 宽度传 0 表示不做换行约束
local sizeNoWrap = textService:GetTextSize(content, 30, Enums.TextFontType.HKXZYT_W9_GB, Vector2.New(0, 0))
print(string.format("不限宽尺寸: %.2f x %.2f", sizeNoWrap.x, sizeNoWrap.y))
-- 限宽 80 触发折行,返回的高度会随之增大
local sizeWrap = textService:GetTextSize(content, 30, Enums.TextFontType.HKXZYT_W9_GB, Vector2.New(80, 0))
print(string.format("限宽 80 尺寸: %.2f x %.2f", sizeWrap.x, sizeWrap.y))
-- 用两次测量结果判断是否发生了折行
print("是否折行:", sizeWrap.y > sizeNoWrap.y)

GetTextBoundsAsync ​

签名:GetTextBoundsAsync(params: Table) -> Vector2 (文本宽高)

按参数表计算文本的显示尺寸,返回文本的宽高(Vector2)。参数为一个表,用于描述待测量文本及其排版条件。

参数类型说明
paramsTable参数表 {Text, Font, Size, Width}

返回值 Vector2 (文本宽高)

示例代码

按参数表测量文本尺寸并对比不同换行宽度

lua
-- @runtime client
-- 获取文本服务实例:测量类接口仅客户端返回有效结果
local textService = game:GetService("TextService")
local content = "Hello TextService"
-- 按参数表测量:Text 为文本、Font 为字体、Size 为字号、Width 为换行宽度
local bounds = textService:GetTextBoundsAsync({
    Text = content,
    Font = Enums.TextFontType.HKXZYT_W9_GB, -- 字体族
    Size = 30, -- 字号
    Width = 200, -- 较宽的换行约束
})
print(string.format("宽约束 200 的文本尺寸: %.2f x %.2f", bounds.x, bounds.y))
-- 收窄换行宽度后再次测量,对比高度变化
local narrowBounds = textService:GetTextBoundsAsync({
    Text = content,
    Font = Enums.TextFontType.YUANTI_BOLD, -- 换成粗圆体
    Size = 30,
    Width = 80, -- 更窄的换行约束会触发折行
})
print(string.format("宽约束 80 的文本尺寸: %.2f x %.2f", narrowBounds.x, narrowBounds.y))

GetTextSizeOffsetAsync ​

签名:GetTextSizeOffsetAsync(fontSize: Int, font?: String) -> Float (偏移量,恒为 0)

接收字号与可选字体,返回一个 Float 类型的文字大小偏移量。

参数类型说明
fontSizeInt字号
font?String字体

返回值 Float (偏移量,恒为 0)

示例代码

查询字号对应的高度偏移并用于行高计算

lua
-- @runtime client
-- 获取文本服务:该接口用于查询玩家首选字号带来的高度偏移
local textService = game:GetService("TextService")
local fontSize = 30
-- 查询指定字号与字体下的高度偏移量
local offset = textService:GetTextSizeOffsetAsync(fontSize, Enums.TextFontType.HKXZYT_W9_GB)
print("字号:", fontSize)
print("高度偏移:", offset)
-- 布局时把偏移叠加到字号上,得到实际可用的行高
local lineHeight = fontSize + offset
print("布局用行高:", lineHeight)

GetFamilyInfoAsync ​

签名:GetFamilyInfoAsync(assetId?: Any) -> Table (字体族信息 {Name, Faces})

TextService 的 GetFamilyInfoAsync 用于查询字体族信息,参数 assetId 可选,返回一个包含 Name 与 Faces 字段的表。

参数类型说明
assetId?Any字体表 ID 或字体资源名

返回值 Table (字体族信息 {Name, Faces})

示例代码

查询字体族信息并读取名称与字形列表

lua
-- @runtime client
-- 获取文本服务,用于查询字体族信息
local textService = game:GetService("TextService")
-- 不传 assetId 时按默认字体查询字体族信息
local familyInfo = textService:GetFamilyInfoAsync()
print("字体族信息:", familyInfo)
-- 返回值为表,先做类型检查再读取内部字段,避免布局代码报错
if type(familyInfo) == "table" then
	print("字体族名称:", familyInfo.Name)
	local faces = familyInfo.Faces
	print("字形列表类型:", type(faces))
end

FilterStringAsync ​

签名:FilterStringAsync(stringToFilter: String, fromUserId?: Int, textContext?: String) -> String (过滤后的文本)

TextService 的文本过滤接口,接收待过滤的字符串,并可选传入发起者用户 Id 与文本上下文,返回过滤后的文本字符串。

参数类型说明
stringToFilterString待过滤文本
fromUserId?Int发送者 ID,当前仅占位
textContext?String审核场景,省略按发布场景审

返回值 String (过滤后的文本)

示例代码

过滤玩家输入文本后再展示

lua
-- @runtime client
-- 获取文本服务:过滤接口走内容审核,审核不可用时原样返回文本
local textService = game:GetService("TextService")
-- 模拟一段待展示给其他玩家的输入文本
local rawText = "这是一段玩家输入的文本"
-- 调用过滤接口拿到脱敏后的文本,可安全用于界面展示
local filteredText = textService:FilterStringAsync(rawText)
print("原始文本:", rawText)
print("过滤后文本:", filteredText)
-- 后续逻辑用过滤结果做长度校验,避免原始文本直接进入展示链路
print("原始长度:", #rawText, "过滤后长度:", #filteredText)