富文本 richText 分段加载 ColorFont.lua

来源:互联网 发布:淘宝全网举报会怎么样 编辑:程序博客网 时间:2024/06/16 17:30
module("ColorFont",package.seeall)


local DEFAULT_FONT = "Droid Sans Fallback" -- "Droid Sans Fallback" "ttf/font.ttf" 在挂机的 config.lua 里


--和挂机不同,这里的颜色字采用分页


function create_label(text, fontSize) --注释的为挂机源码
--    if MY_LANGUAGE == LANGUAGE_TYPE.LANGUAGE_CH then
--        return cc.Label:createWithSystemFont( text, DEFAULT_FONT, size)
--    else
--        return cc.Label:createWithTTF( text, DEFAULT_FONT, size)
--    end
    return cc.Label:createWithSystemFont( text, DEFAULT_FONT, fontSize)
end


function get_paragraphs(content)
    return string.split(content, "[/br]")
end


-- iPar = last index of paragraphs, iRep = last index of replace, num = 加载多少段(number of paragraphs)
function get_rich_texts(paragraphs, replace, width, fontSize, iPar, iRep, num) 
    if iPar == #paragraphs then
        return nil
    end
    local start = iPar + 1
    local bound = math.min(#paragraphs, iPar + num)
    iPar = bound
    local node = cc.Node:create()
    local nowHeight = 0
    for i = start, bound do
        local p = string.gsub(paragraphs[i], '#s#', function() 
            iRep = iRep + 1
            if replace[iRep] ~= nil then
                local replace_type = type(replace[iRep])
                if replace_type == 'string' or replace_type == 'number' then
                    return replace[iRep]
                else 
                    return ''
                end
            end
            return ''
        end)
        local richText, textSize = get_rich_label(paragraphs[i], width, fontSize )
        nowHeight = nowHeight + textSize.height
        richText:setAnchorPoint(cc.p(0.25, 0))     -- cc.p(0.25, ...) ??
        richText:setPosition(cc.p(0, -nowHeight))
        node:addChild(richText)
    end
    return node, nowHeight, iPar, iRep
end


function get_rich_label(_p, width, fontSize ) -- current paragraph
    local richText = ccui.RichText:create()
    richText:ignoreContentAdaptWithSize(false)
    richText:setContentSize(cc.size(width, 0))
    local fragments = format_paragraph(_p) -- 当前段产生的信息碎片集合 
    for i = 1, #fragments do
        if fragments[i].color then
            fontSize = math.floor(fontSize * fragments[i].color[2] * 1.0 / 100)
            break
        end
    end
    local lbl = create_label("", fontSize)
    local str = ""
    lbl:setDimensions(width, 0)
    for i = 1, #fragments do
        str = str .. fragments[i].text
    end
    lbl:setString(str)
    local color = ''
    local textSize = lbl:getContentSize()
    for i = 1, #fragments do
        if fragments[i].color then
            color = GlobalVar[fragments[i].color[1]]
        else
            color = GlobalVar['white0']
        end
        local richElem = ccui.RichElementText:create(0, color, 255, fragments[i].text, DEFAULT_FONT, fontSize)
        richText:pushBackElement(richElem) 
    end
    if #fragments > 0 and fragments[1].text == "[/line]" then
        richText:setVisible(false)
    end
    return richText,textSize
end


function format_paragraph(_p) -- current paragraph
    local ret = {}
    while true do
        local start1, end1, color, fontSize = string.find(_p, '%[color=(%a+%d+),(%d+)%]')
        if start1 == nil then
            break
        end
        local start2, end2 = string.find(_p, "%[/color%]", end1 + 1)
        local str1 = string.sub(_p, 1, start1 - 1)
        local str2 = string.sub(_p, end1 + 1, start2 - 1)
        if str1 ~= '' then
            table.insert(ret, {text = str1})
        end
        if str2 ~= '' then
            table.insert(ret, {text = str2, color = {color, fontSize}})
        end
        _p = string.sub(_p, end2 + 1)
    end
    if _p ~= '' then
        table.insert(ret,{text = _p})
    end
    return ret
end
0 0