在cocos2d-x-3.3beta0上设置背景颜色文本字段

时间:2015-03-04 10:21:55

标签: cocos2d-iphone cocos2d-x

我使用cocos2d-x-3.3beta0使用Text类创建文本。

Text* MessageText = Text::create(message, FONT, FONT_SIZE);

我已设置文字区域的大小

MessageText ->setTextAreaSize(Size(100,150));

现在我要为文本区域着色,这是文本的背景。有没有办法做到这一点?

由于

2 个答案:

答案 0 :(得分:2)

UIText中没有背景颜色属性。您可以对UIText进行子类化,并在其后面添加一个Layout小部件以用作背景。

<强> TextWithBackground.h

//
//  TextWithBackground.h
//
//  Created by Baris Atamer on 3/5/15.
//
//

#ifndef __TextSample__TextWithBackground__
#define __TextSample__TextWithBackground__

#include "cocos2d.h"
#include "extensions/cocos-ext.h"
#include "ui/CocosGUI.h"

USING_NS_CC;
USING_NS_CC_EXT;

using namespace ui;

class TextWithBackground : public Text
{
public:

    static TextWithBackground* create();
    static TextWithBackground* create(const std::string& textContent,
                                       const std::string& fontName,
                                       int fontSize);

    virtual bool init();
    virtual bool init(const std::string &textContent, const std::string &fontName, int fontSize);

    void setBackgroundColor(const Color3B& color);
    void setString(const std::string& text);

protected: // Variables
    Layout *_background;

};

#endif /* defined(__TextSample__TextWithBackground__) */  

<强> TextWithBackground.cpp

//
//  TextWithBackground.cpp
//
//  Created by Baris Atamer on 3/5/15.
//
//

#include "TextWithBackground.h"

TextWithBackground* TextWithBackground::create()
{
    TextWithBackground* widget = new (std::nothrow) TextWithBackground();
    if (widget && widget->init())
    {
        widget->autorelease();
        return widget;
    }
    CC_SAFE_DELETE(widget);
    return nullptr;
}

bool TextWithBackground::init()
{
    if ( !Text::init() ) return false;

    _background = Layout::create();
    _background->setBackGroundColorType(cocos2d::ui::Layout::BackGroundColorType::SOLID);
    addChild(_background,-1);

    return true;
}

bool TextWithBackground::init(const std::string &textContent, const std::string &fontName, int fontSize)
{
    bool ret = true;
    do
    {
        if (!Text::init())
        {
            ret = false;
            break;
        }
        this->setFontName(fontName);
        this->setFontSize(fontSize);
        this->setString(textContent);
    } while (0);
    return ret;
}

void TextWithBackground::setString(const std::string& text)
{
    Text::setString(text);
    _background->setContentSize(getContentSize());
}

void TextWithBackground::setBackgroundColor(const cocos2d::Color3B &color)
{
    _background->setBackGroundColor(Color3B::RED);
    _background->setContentSize(getContentSize());
}

现在你有了setBackground方法。

TextWithBackground *MessageText = TextWithBackground::create();
MessageText->setBackgroundColor(Color3B::YELLOW);
MessageText->setString("Text with a background ");
addChild(MessageText);

答案 1 :(得分:1)

Text *myTexxtt = Text::create("Hyee Shaq", "Marker Felt.ttf", 100);
myTexxtt->setPosition(Point(WinSize.width/2,WinSize.height/2));
myTexxtt->setColor(Color3B::RED);
this->addChild(myTexxtt,10);