for i, v in pairs(attrDescs.desc) do
SL:print(v.text)
if v.text then
index = index + 1
local label_posName = GUI:RichTextFCOLOR_Create(panel, "desc_text_" .. index, 0, 0, v.text or "", suitWidth - 20, _setFontSize or _DefaultFSize, SL:GetColorByStyle
根据浮生梦老师前端课程内容,要实现这个功能,我们需要先解析`attrDescs.desc`中的文本,然后根据`meetCount`的值来动态替换颜色标签。
```lua
-- 首先获取套装属性描述
local attrDescs = GUIFunction:GetParseItemDesc(suitConfig.equipAttStr)
-- 处理描述文本,根据meetCount替换颜色
local function processDescText(text, meetCount)
if not text then return text end
-- 根据meetCount判断要使用的颜色
local targetColor = 13 -- 默认替换为13
-- 如果meetCount小于2,将所有250和251替换为13
if meetCount < 2 then
text = string.gsub(text, "/FCOLOR=250", "/FCOLOR=13")
text = string.gsub(text, "/FCOLOR=251", "/FCOLOR=13")
-- 如果meetCount小于4,只替换攻击相关的行
elseif meetCount < 4 then
if string.find(text, "<4/FCOLOR=250>") then
text = string.gsub(text, "/FCOLOR=250", "/FCOLOR=13")
text = string.gsub(text, "/FCOLOR=251", "/FCOLOR=13")
end
-- 如果meetCount小于6,只替换生命相关的行
elseif meetCount < 6 then
if string.find(text, "<6/FCOLOR=250>") then
text = string.gsub(text, "/FCOLOR=250", "/FCOLOR=13")
text = string.gsub(text, "/FCOLOR=251", "/FCOLOR=13")
end
-- 如果meetCount小于7,只替换暴击相关的行
elseif meetCount < 7 then
if string.find(text, "<7/FCOLOR=250>") then
text = string.gsub(text, "/FCOLOR=250", "/FCOLOR=13")
text = string.gsub(text, "/FCOLOR=251", "/FCOLOR=13")
end
end
return text
end
-- 处理attrDescs.desc中的每个条目
if attrDescs and attrDescs.desc and next(attrDescs.desc) then
for i, v in pairs(attrDescs.desc) do
if v.text then
-- 处理文本,根据meetCount替换颜色
v.text = processDescText(v.text, meetCount)
index = index + 1
local label_posName = GUI:RichTextFCOLOR_Create(
panel,
"desc_text_" .. index,
0,
0,
v.text or "",
suitWidth - 20,
_setFontSize or _DefaultFSize,
SL:GetHexColorByStyleId(255), -- 注意:这里应该用GetHexColorByStyleId
vspace,
nil,
fontPath
)
GUI:setAnchorPoint(label_posName, 0, 0)
GUI:setTag(label_posName, index)
local size = GUI:getContentSize(label_posName)
heights[index] = size.height + interval
richWidth = math.max(richWidth, size.width)
elseif v.res then
index = index + 1
local img = ItemTips.GetNodeImage(panel, v, index)
if img then
GUI:setTag(img, index)
local size = GUI:getContentSize(img)
heights[index] = size.height + interval
richWidth = math.max(richWidth, size.width)
end
end
end
end
```
**注意:**
1. 我注意到你原来的代码中使用了`SL:GetColorByStyleId(255)`,但在浮生梦老师的前端课程中,获取颜色应该使用`SL:GetHexColorByStyleId()`函数
2. 函数说明:
- `string.gsub(text, pattern, replacement)`:Lua字符串替换函数
- `string.find(text, pattern)`:查找字符串中是否包含指定模式
- `GUI:RichTextFCOLOR_Create()`:创建带颜色格式的富文本控件
- `GUI:getContentSize()`:获取控件内容大小
这个实现会根据`meetCount`的值动态替换不同行的颜色标签,完全按照你的要求来处理的。