|
楼主 |
发表于 2023-11-25 12:47:01
|
显示全部楼层
function wrapTextByWidth(text, maxWidth)
local lines = {}
local line = ""
local i = 1
local textLength = utf8.len(text)
local lineWidth = 0
local function handleWidth(char)
local charWidth = utf8.width(char, true, 1)
lineWidth = lineWidth + charWidth
if lineWidth <= maxWidth then
line = line .. char
else
lineWidth = charWidth
table.insert(lines, line)
line = char
end
i = i + 1
end
while i <= textLength do
local char = utf8.sub(text, i, i)
if char == "<" then
local endBracketIndex = utf8.find(text,">", i + 1)
if endBracketIndex then
local colorCode = utf8.sub(text, i, endBracketIndex)
if utf8.find(colorCode, "%d+%p%d+") or colorCode == "" then
line = line .. colorCode
i = endBracketIndex + 1
else
handleWidth(char)
end
else
i = i + 1
end
else
handleWidth(char)
end
end
table.insert(lines, line)
local result = table.concat(lines, "\n")
local empty_space = utf8.match(result, "<%d+%p%d+%p%d+%p%d+%p%d+%p%d+>%s$")
if empty_space then
result = utf8.sub(result, 1, -#empty_space - 1)
end
return result
end
我今天更新了一版 |
|