lua html式解析

function MainScene:parseString(str, spTb)
    
    local function Split(szFullString, szSeparator)
        local nFindStartIndex = 1
        local nSplitIndex = 1
        local nSplitArray = {}
        while true do
           local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
           if not nFindLastIndex then
            nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
            break
           end
           nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
           nFindStartIndex = nFindLastIndex + string.len(szSeparator)
           nSplitIndex = nSplitIndex + 1
        end
        return nSplitArray
    end


    local function createMyRichText(fieldSize,spTb)
        local rc=ccui.RichText:create():pos(100, 200)
        rc:ignoreContentAdaptWithSize(false)
        rc:setContentSize(fieldSize)
        for key, var in pairs(spTb) do
            var:setContentSize(var:getBoundingBox())
            local rcItem=ccui.RichElementCustomNode:create(key,cc.c3b(0,255,255),255,var)
            rc:pushBackElement(rcItem)
        end
        return rc
    end




    local t = string.sub(str, 2, 2)
    local modeString = string.format("<%s.-</%s>", t, t)
    local w = string.match(str, modeString)
    if w == nil then
        local richText=createMyRichText(cc.size(500,400), spTb)
        richText:setPosition(cc.p(480,120))
        self:addChild(richText)
       return 
    end


    local as = string.find(w, ">")
    local bs = string.find(w, "</"..t..">")
    local p = string.sub(w, as+1, bs-1)
    
    local cl = string.match(w, "%d+,%d+,%d+")
    local co = cc.c3b(255, 255, 255)
    if cl ~= nil then
        local dcl = Split(cl, ",")
        co = cc.c3b(dcl[1], dcl[2], dcl[3])
    end
    local text = cc.Label:createWithSystemFont(p, "宋体", 30)
    text:setColor(co)


    if t == "b" then
        text = cc.Label:createWithSystemFont(p, "黑体", 30)
        text:setColor(co)
    elseif t == "l" then
        text:setSkewX(20)
    end
    table.insert(spTb, text)


    local leftString = string.sub(str, string.len(w)+1, -1)
    self:parseString(leftString, spTb)


end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值