某個csv中有個string字段是這樣的
1、每日快速戰鬥 <font color='#9ced4d'> 3 </font>次<br>2、每日可領取銀幣×5000<br>3、招財次數<font color='#9ced4d'> 10 </font>次<br>4、競技場購買次數<font color='#9ced4d'> 5 </font>次<br>5、世界BOSS鼓舞效果<font color='#9ced4d'> 2% </font><br>6、千層塔重置次數<font color='#9ced4d'> 1 </font>次<br>7、材料副本購買次數 <font color='#9ced4d'> 1 </font>次<br>8、<font color='#9ced4d'>“一鍵升階神器功能”</font><br>
使用lua讀取的時候,整理了一個RichText,專門用來讀這個
注:COMPONENT_CONFIG字段出處不詳,不知道該帖誰的鏈接
--專門解析用
local RichText = class("RichText", function ()
return ccui.RichText:create()
end)
local NORMAL_TEXT = 0
local FONT_COMPONENT = 1 -- 不同顏色
local NEW_LINE_COMPONENT = 2 -- 換行<br>
local COMPONENT_CONFIG = {
[FONT_COMPONENT] = {
-- ["all"] = "<%s-b.-><%s-font.->.-</%s-font%s-></%s-b->",
["all"] = "<%s-font.->.-</%s-font%s->",
["color"] = "color%s-=%s-([%a_]+)%s-",
["size"] = "size%s-=%s-(%d+)%s-",
["content"] = "<%s-font.->(.*)</%s-font%s->",
},
[NEW_LINE_COMPONENT] = {
["all"] = "<%s-[bB][rR]%s->",
},
}
function RichText:ctor( params )
params = params or {}
self.m_components = {}
self.m_color = params.color or cc.c3b(80, 255, 80)
self.m_size = params.size or 26
end
function RichText:setText( _content )
self.m_content = _content
self:update()
end
function RichText:update( ... )
local PATTERN_CONFIG = COMPONENT_CONFIG
self.m_components = {}
local totalLen = string.len( self.m_content )
local st = 0
local en = 0
for i = 1, #PATTERN_CONFIG, 1 do
st = 0
en = 0
while true do
st, en = string.find( self.m_content, PATTERN_CONFIG[i]["all"], st + 1 )
if not st then
break
end
local comp = {}
comp.sIdx = st
comp.eIdx = en
comp.type = i
comp.text = string.sub( self.m_content, comp.sIdx, comp.eIdx )
table.insert( self.m_components, comp )
st = en
end
end
local function sortFunc( a, b )
return a.sIdx < b.sIdx
end
table.sort( self.m_components, sortFunc )
if #self.m_components <= 0 then
local comp = {}
comp.sIdx = 1
comp.eidx = totalLen
comp.type = NORMAL_TEXT
comp.text = self.m_content
table.insert( self.m_components, comp )
else
local offset = 1
local newComponents = {}
for i = 1, #self.m_components, 1 do
local comp = self.m_components[ i ]
table.insert( newComponents, comp )
if comp.sIdx > offset then
local newComp = {}
newComp.sIdx = offset
newComp.eIdx = comp.sIdx - 1
newComp.type = NORMAL_TEXT
newComp.text = string.sub( self.m_content, newComp.sIdx, newComp.eIdx )
table.insert( newComponents, newComp )
end
offset = comp.eIdx + 1
end
if offset < totalLen then
local newComp = {}
newComp.sIdx = offset
newComp.eIdx = totalLen
newComp.type = NORMAL_TEXT
newComp.text = string.sub( self.m_content, newComp.sIdx, newComp.eIdx )
table.insert( newComponents, newComp )
end
self.m_components = newComponents
end
table.sort( self.m_components, sortFunc )
-- dump(self.m_components)
self:render()
self:formatText()
end
function RichText:render()
for i = 1, #self.m_components, 1 do
local comp = self.m_components[i]
local text = comp.text
if comp.type == NORMAL_TEXT then
self:handleNormalTextRender( text )
elseif comp.type == FONT_COMPONENT then
self:handleFontTextRender( text )
elseif comp.type == NEW_LINE_COMPONENT then
self:handleNewLineRender()
end
end
end
function RichText:handleNormalTextRender( _text )
local color = cc.c3b(255, 255, 255)
-- print("--一個無效參數---",color,_text,display.DEFAULT_TTF_FONT,self.m_size)
local element = ccui.RichElementText:create(1, color, 255, _text or "", "res/simhei.ttf", self.m_size)
self:pushBackElement( element )
end
function RichText:handleFontTextRender( _text )
local content = ""
local color = self.m_special_color or cc.c3b(80, 255, 80)
local size = self.m_size or 26
content = string.match( _text, COMPONENT_CONFIG[ FONT_COMPONENT ]["content"] )
--如果csv裏打算設定大小
-- color = string.match( _text, COMPONENT_CONFIG[ FONT_COMPONENT ]["color"] )
-- size = string.match( _text, COMPONENT_CONFIG[ FONT_COMPONENT ]["size"] )
-- print("-- 另一個無效參數--",color,content,display.DEFAULT_TTF_FONT,size)
local element = ccui.RichElementText:create(1, color, 255, content, "res/simhei.ttf", size)
self:pushBackElement( element )
end
function RichText:handleNewLineRender( ... )
local color = cc.c3b(255,255,255)
local element = ccui.RichElementNewLine:create(1, color, 255)
self:pushBackElement( element )
end
return RichText