• cocos2d-x之利用富文本控件遍历xml


    1.

    #ifndef SuperRichText_hpp

    #define SuperRichText_hpp

    #include <stdio.h>

    #include "cocos2d.h"

    #include "ui/UIRichText.h"

    #include "tinyxml2/tinyxml2.h"

    USING_NS_CC;

    struct FontInfo{

        std::string fontName;

        float fontSize;

        Color3B color;

        GLubyte opacity;

    };

    class SuperRichText:public ui::Widget

    {

    protected:

        std::vector<FontInfo> _fontList;

        std::vector<ui::RichText*> _lineList;

        ui::RichText* _line;

    public:

        SuperRichText();

        virtual ~SuperRichText();

        CREATE_FUNC(SuperRichText);

        virtual void renderHtml(const char* html);

        virtual void renderNode(tinyxml2::XMLNode* node);

        void addNewLine();

        void updateLine();

    };

    #endif /* SuperRichText_hpp */

    #include "SuperRichText.hpp"

    SuperRichText::SuperRichText(){

        FontInfo defaultFont;

        defaultFont.color=Color3B(255,255,255);

        defaultFont.fontSize=24.0f;

        defaultFont.opacity=255;

        defaultFont.fontName="";

        

        _fontList.push_back(defaultFont);

        

        addNewLine();

    }

    SuperRichText::~SuperRichText(){

        this->removeAllChildren();

    }

    void SuperRichText::renderHtml(const char *html){

        tinyxml2::XMLDocument xml;

        xml.Parse(html);

        renderNode(xml.FirstChild());

        updateLine();

    }

    void SuperRichText::renderNode(tinyxml2::XMLNode *node){

        while (node!=nullptr) {

            if (node->ToText()) {

                CCLOG("文本信息:%s",node->ToText()->Value());

            }else if (node->ToElement()){

                auto n=node->ToElement();

                std::string name=n->Name();

                

                std::transform(name.begin(),name.end(),name.begin(),::toupper);

                

                if (name=="FONT") {

                    CCLOG("字体标签");

                    renderNode(n->FirstChild());//继续渲染子集

                }else if (name=="IMG") {

                    CCLOG("图片标签");

                }else if (name=="BR") {

                    CCLOG("换行标签");

                }

            }

            node=node->NextSibling();

        }

    }

    void SuperRichText::addNewLine(){

        _line=ui::RichText::create();

        _lineList.push_back(_line);

        

        addChild(_line);

    }

    void SuperRichText::updateLine(){

        

    }

    2.

    bool HelloWorld::init()

    {

        if ( !Layer::init() )

        {

            return false;

        }

        

        Size visibleSize = Director::getInstance()->getVisibleSize();

        Vec2 origin = Director::getInstance()->getVisibleOrigin();

        

        auto richText=SuperRichText::create();

        

        richText->setPosition(visibleSize/2);

        

        richText->renderHtml("

    <font color='ffffff' size='60' opacity='255'>

    你好<img src='CloseNormal.png'/>daochong

    <br/>

    </font>");

        

        return true;

    }

  • 相关阅读:
    poj-2478 Farey Sequence(dp,欧拉函数)
    codeforces 515C C. Drazil and Factorial(水题,贪心)
    hdu-5643 King's Game(打表)
    可以在命令行直接使用密码来进行远程连接和远程拉取文件的命令:sshpass
    查看SQL运行时间
    管理kafka
    No module named _sqlite3
    ps
    Redis之RDB与AOF
    Redis 4 参数解释
  • 原文地址:https://www.cnblogs.com/daochong/p/5270889.html
Copyright © 2020-2023  润新知