local elements = {}
local element = GUI:RichTextCombineCell_Create(-1, "show", 0, 0, "TEXT", {
str = "第一行文本11111",
color = "#FF0000",
fontSize = 16
})
table.insert(elements, element)
GUI:Ric
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-03-31 23:50:52 |
👁️ 阅读: 2
根据浮生梦老师前端课程内容,我来帮你创建一个专门处理富文本格式的函数。
首先,你需要创建一个富文本控件作为容器。这里我假设你已经有一个父节点 `parent`,并创建了富文本控件 `ui.RichText_1`。
```lua
-- 创建富文本控件(假设在某个界面初始化时创建)
-- parent 是你的界面父节点,例如:local parent = GUI:Win_Create("MyWindow", ...)
-- ui.RichText_1 = GUI:RichText_Create(parent, "RichText_1", x, y, "", width, nil, nil, vspace)
-- 定义一个函数来格式化和显示你的info文本
local function formatAndShowLevelInfo(richTextWidget, infoText)
-- 清空之前的元素(如果需要的话,可以先清空控件内容)
-- 注意:知识库中没有直接的清空函数,这里我们通过重新设置空字符串来模拟清空
-- GUI:RichText_setString 如果存在的话可以清空,但知识库未提供,所以我们用新的elements覆盖
local elements = {}
-- 使用字符串分割,按行处理
local lines = SL:Split(infoText, "\n")
for _, line in ipairs(lines) do
local color = "#FFFFFF" -- 默认白色
local fontSize = 16
local isBold = false
-- 根据内容判断样式
if string.find(line, "【") then
-- 标题行,使用亮色
color = "#FFD700" -- 金色
fontSize = 18
isBold = true
elseif string.find(line, "经验倍数") or string.find(line, "提示:") then
-- 小标题,使用青色
color = "#00FFFF" -- 青色
fontSize = 17
isBold = true
elseif string.find(line, "倍经验") then
-- 经验倍数行,使用绿色突出数字
color = "#00FF00" -- 绿色
fontSize = 16
elseif string.find(line, "善用") or string.find(line, "配合") then
-- 提示内容,使用黄色
color = "#FFFF00" -- 黄色
fontSize = 15
end
-- 创建富文本元素
local element = GUI:RichTextCombineCell_Create(-1, "line_" .. _, 0, 0, "TEXT", {
str = line,
color = color,
fontSize = fontSize,
isBold = isBold
})
table.insert(elements, element)
-- 添加换行(除了最后一行)
if _ < #lines then
local newline = GUI:RichTextCombineCell_Create(-1, "newline_" .. _, 0, 0, "NEWLINE", {})
table.insert(elements, newline)
end
end
-- 将元素添加到富文本控件
GUI:RichTextCombine_pushBackElements(richTextWidget, elements)
-- 格式化显示
GUI:RichTextCombine_format(richTextWidget)
end
-- 你的info文本
local info = [[
【1-10级】新手村打鸡鹿稻草人,熟悉操作与技能。
【11-20级】天然洞穴、骷髅洞,挑战初级怪积累经验。
【21-25级】比奇矿区、封魔谷、沃玛寺,开启装备收集。
【26-35级】蜈蚣洞、石墓、尸魔洞、骨魔洞,怪密经验厚。
【36-45级】石墓阵、祖玛寺、牛魔寺、赤月峡谷,高阶副本等你征。
【经验倍数】:
1-21级:20倍经验(新手黄金期)
22-34级:12倍经验(快速成长)
35-38级:3倍经验
39-52级:2倍经验
53-60级:1倍经验(稳定冲刺)
提示:
善用双倍经验地图(幻境、沙巴克藏宝阁、玛雅神殿)
配合盟重老兵传送,效率翻倍!
]]
-- 使用示例:
-- 假设你的富文本控件是 ui.RichText_1
-- formatAndShowLevelInfo(ui.RichText_1, info)
```
**函数说明:**
1. `formatAndShowLevelInfo` 函数接收富文本控件和要显示的文本
2. 自动按行分割文本,根据内容关键词设置不同的颜色和字体大小
3. 使用 `GUI:RichTextCombineCell_Create` 创建文本元素
4. 使用 `GUI:RichTextCombine_pushBackElements` 添加元素
5. 最后调用 `GUI:RichTextCombine_format` 格式化显示
**颜色方案:**
- 标题(【】括起来的):金色 `#FFD700`,18号字,加粗
- 小标题("经验倍数"、"提示:"):青色 `#00FFFF`,17号字,加粗
- 经验倍数行:绿色 `#00FF00`,16号字
- 提示内容:黄色 `#FFFF00`,15号字
- 其他:白色 `#FFFFFF`,16号字
这样你只需要调用一次函数,就能自动格式化和显示所有内容了!
← 返回问答库