富文本响应url整体设计思路

2022-07-28  本文已影响0人  许彦峰

c++的实现细节

handleOpenUrl为a标签点击后的回调函数

RichText* RichText::createWithXML(const std::string& xml, const ValueMap& defaults, const OpenUrlHandler& handleOpenUrl)
{
    RichText* widget = new (std::nothrow) RichText();
    if (widget && widget->initWithXML(xml, defaults, handleOpenUrl))
    {
        widget->autorelease();
        return widget;
    }
    CC_SAFE_DELETE(widget);
    return nullptr;
}

lua层使用

注意第三个参数

    local richText = ccui.RichText:create();
    richText:initWithXML(self:testXMLString(), {}, function(url)
        ---  url来自a标签的href属性
        print("url:", url)
        local app = cc.Application:getInstance()
        app:openURL(url)
    end);

封装1:没有地方在使用

function _M.createRichTextToTarget(parent, label, str, param)
    -- ...注意param的func属性
    if param.func then
        richText:initWithXML(str, defaults, func)
    else
        richText:initWithXML(str, defaults)
    end
    -- ...
end

封装2:大量使用

function _M.addRichText(label, str, func, ignore, isSystemFont, param)
    -- 注意第三个func参数
    if func then
        richText:initWithXML(str, defaults, func)
    else
        richText:initWithXML(str, defaults)
    end
end
-- <a href='%s'><u>X %s, Y %s</></>
-- <a href='100,200'><u>X 100, Y 200</></>
richText = Common.addRichText(node.info.TextContent, text, handler(self, self.onClickSharePos), nil)
function _M:onClickSharePos(str)
    local data = str.split(str, ",")
    if #data == 2 then
        self:close()
        SocketMgr:sendInnerMsg(InnerProtocol.innerMove2CamPve, {gridId = MapUtil:getGridIdByPos(tonumber(data[1]), tonumber(data[2])), time=0.2})
    end
end
txt = string.format("<a href='target'><u>%s</u></a>", txt) --下划线 href写死了
local lineClickEvent = function()
end
Common.addRichText(self._csbControl.renwu_shu.texttask, txt, function()
            lineClickEvent()
        end, false)

封装3:没有地方在使用

function _M.createRichText(str, func)
    local richText = ccui.RichText:create()
    richText:initWithXML(str, {}, func)
    return richText
end

protocol协议

href的完整格式

protocol://host/path

protocol host path search
file
ftp
http
https
ws
wss
app
game bag show-item id="001"
<font>
  <a href="game://bag/show-item?id=001">青龙偃月刀</a>
  <a href="game://bag/show-item?id=002">如意金箍棒</a>
</font>
<font><a href="%s">%s</a></font>
local chat = {
    exec = function(action, param)

    end
}
local URL = function(url)
    local protocol
    local host
    local path
    local search
    if protocol == 'http' or protocol == 'https' then
        local app = cc.Application:getInstance()
        app:openURL(url)
    elseif protocol == "game" then
        if host == "bg" then
            -- bg.exec(path, search)
        elseif host == 'chat' then
            -- path=jumpMap
            -- search="pos=100,200"
            chat.exec(path, search)
        end
    end
end

local function instanceRichtext(xml)
    local richText = ccui.RichText:create();
    scene:addChild(richText)
    richText:initWithXML(xml, {}, function(url)
        URL(url)
    end);
end
上一篇下一篇

猜你喜欢

热点阅读