在 cocos2d-x-3.3beta0 上设置背景色文本字段
Set background color Text Field on cocos2d-x-3.3beta0
我正在使用文本 class 创建文本,如下所示,使用 cocos2d-x-3.3beta0。
Text* MessageText = Text::create(message, FONT, FONT_SIZE);
我已经设置了文本区域的大小
MessageText ->setTextAreaSize(Size(100,150));
现在我想给文本区域上色,这是文本的背景。有什么办法吗?
谢谢
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);
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);
我正在使用文本 class 创建文本,如下所示,使用 cocos2d-x-3.3beta0。
Text* MessageText = Text::create(message, FONT, FONT_SIZE);
我已经设置了文本区域的大小
MessageText ->setTextAreaSize(Size(100,150));
现在我想给文本区域上色,这是文本的背景。有什么办法吗?
谢谢
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);
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);