参考 › 函数参考 › Functions

Functions.text.wrap

公共

调用方式

Functions.text.wrap(str, limit)

将其记录为名为 Functions.text.wrap 的依赖项(接受自动补全即可完成),然后按该名称调用。

也接受以前的命名空间 utils 下的名称。

作用

在接近目标列数的空白边界处对文本换行。长单词会保持完整。

参数

名称类型作用
strstring要换行的文本。
limitnumber | nil目标行宽,默认为 72 个字符。

返回值

1string在单词边界处插入换行的文本。

版本

内置辅助函数在每个版本上都会注册。不同的是它们读取的游戏 API;某个 API 缺失时,辅助函数会改用后备方案,或返回 nil 并附上原因。

它不直接读取任何游戏 API。

示例

local note = Functions.text.wrap("Use defensive cooldowns before the tank swap", 20)
local _, breaks = note:gsub("\n", "")
return breaks >= 1

源码

Functions/text.lua,第 37 行。在游戏中为只读;“复制”会生成一份可编辑的副本。

Functions.text.wrap(str, limit)
    limit = limit or 72
    local here = 1

    -- the "".. is there because :gsub returns multiple values
    return ""..str:gsub("(%s+)()(%S+)()",
            function(_, st, word, fi)
                if fi-here > limit then
                    here = st
                    return "\n"..word
                end
            end)

返回函数参考