首页 > 代码库 > cocos2d-x3.0 RichText

cocos2d-x3.0 RichText


.h

#include "cocos2d.h"
#include "cocos-ext.h"
#include "ui/CocosGUI.h"
#include "cocostudio/CocoStudio.h"
USING_NS_CC;

USING_NS_CC_EXT;
using namespace ui;


RichText* _richText;
    
    
    void  touchEvent(Ref *pSender, TouchEventType type);

.cpp

  layout = Layout::create();
        layout->setSize(Size(widgetSize.width, widgetSize.height));
       
        //横向排列,这里相似Android里的线性布局
        //layout->setLayoutType(LAYOUT_RELATIVE);
        /*以图片为背景*/
        layout->setBackGroundImageScale9Enabled(true);
        layout->setBackGroundImage("green_edit.png");
        
        layout->setPosition(Point(0,0));
        addChild(layout);

        /******************************************************************************************/
        _richText = RichText::create();
        
        _richText->ignoreContentAdaptWithSize(false);
        _richText->setSize(Size(300, 500));
        
        RichElementText* re1 = RichElementText::create(1, Color3B::WHITE, 255, "This color is White.", "Helvetica", 30);
        RichElementText* re2 = RichElementText::create(2, Color3B::YELLOW, 255, "And this is yellow. ", "Helvetica", 30);
        RichElementText* re3 = RichElementText::create(3, Color3B::BLUE, 255, "This one is blue. ", "Helvetica", 30);
        RichElementText* re4 = RichElementText::create(4, Color3B::GREEN, 255, "And green. ", "Helvetica", 30);
        RichElementText* re5 = RichElementText::create(5, Color3B::RED, 255, "Last one is red ", "Helvetica", 30);
        
        RichElementImage* reimg = RichElementImage::create(6, Color3B::WHITE, 255, "sliderballnormal.png");
        cocostudio::ArmatureDataManager::getInstance()->addArmatureFileInfo("100.ExportJson");
        cocostudio::Armature* pAr = cocostudio::Armature::create("100");
        pAr->getAnimation()->play("Animation1");
        
        RichElementCustomNode* recustom = RichElementCustomNode::create(1, Color3B::WHITE, 255, pAr);
        RichElementText* re6 = RichElementText::create(7, Color3B::ORANGE, 255, "Have fun!!", "Helvetica", 30);
        _richText->pushBackElement(re1);
        _richText->insertElement(re2, 1);
        _richText->pushBackElement(re3);
        _richText->pushBackElement(re4);
        _richText->pushBackElement(re5);
        _richText->insertElement(reimg, 2);
        _richText->pushBackElement(recustom);
        _richText->pushBackElement(re6);
        
        
        _richText->setPosition(Point(widgetSize.width / 2, widgetSize.height / 2));
        _richText->setLocalZOrder(10);
        
        
        
        
        layout->addChild(_richText);
        Button* button = Button::create("animationbuttonnormal.png", "animationbuttonpressed.png");
        button->setTouchEnabled(true);
        button->setTitleText("switch");
        button->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f + button->getSize().height * 2.5));
        button->addTouchEventListener(this, toucheventselector(LayoutTest::touchEvent));
        button->setLocalZOrder(10);
        layout->addChild(button);

void LayoutTest::touchEvent(cocos2d::Ref *pSender, TouchEventType type)
{
    switch (type) {
        case cocos2d::ui::TOUCH_EVENT_BEGAN:
            log("TOUCH_EVENT_BEGAN");
            break;
            
            
            
        case cocos2d::ui::TOUCH_EVENT_MOVED:
            log("TOUCH_EVENT_MOVED");
            break;
            
        case cocos2d::ui::TOUCH_EVENT_ENDED:
            if (_richText->isIgnoreContentAdaptWithSize())
            {
                _richText->ignoreContentAdaptWithSize(false);
                _richText->setSize(Size(300, 500));
            }
            else
            {
                _richText->ignoreContentAdaptWithSize(true);
            }

            break;
        case cocos2d::ui::TOUCH_EVENT_CANCELED:
            log("TOUCH_EVENT_CANCELED");
            break;
            
        default:
            break;
    }
}