转自:http://fengmm521.blog.163.com/blog/static/2509135820138943638777/ 点击打开链接
.h
#ifndef __CCSTROKELABEL_H__
#define __CCSTROKELABEL_H__
#include "cocos2d.h"
#include "label_nodes/CCLabelTTF.h"
class CCStrokeLabel : public cocos2d::CCNode
{
public:
void initString();
static CCStrokeLabel* create(cocos2d::CCLabelTTF* labelTTF,cocos2d::ccColor3B fullColor,cocos2d::ccColor3B StrokeColor,float strokeSize);
public:
float getStrokeSize(){ return m_strokeSize; };
void setStrokeSize(float strokeSize){ m_strokeSize = strokeSize; };
private:
cocos2d::CCSprite* m_sprite;
cocos2d::CCLabelTTF* m_label;
cocos2d::ccColor3B m_fullColor;
cocos2d::ccColor3B m_StrokeColor;
float m_strokeSize;
};
#endif //__CCSTROKELABEL_H__
.cpp
#include "CStrokeLabel.h"
using namespace cocos2d;
CCStrokeLabel* CCStrokeLabel::create(cocos2d::CCLabelTTF* labelTTF,\
cocos2d::ccColor3B fullColor,\
cocos2d::ccColor3B StrokeColor,\
float strokeSize)
{
CCStrokeLabel* tmp = new CCStrokeLabel;
tmp->autorelease();
tmp->m_label = labelTTF;
tmp->m_fullColor = fullColor;
tmp->m_StrokeColor = StrokeColor;
tmp->m_strokeSize = strokeSize;
tmp->initString();
return tmp;
}
void CCStrokeLabel::initString()
{
float strokeSize = getStrokeSize();
// float fontSize = m_fontSize - 2 * strokeSize;
// m_label = CCLabelTTF::labelWithString(
// m_str.c_str(), m_font.c_str(), m_fontSize);
CCSize textureSize = m_label->getContentSize();
textureSize.width += 2 * strokeSize;
textureSize.height += 2 * strokeSize;
//call to clear error
glGetError();
CCRenderTexture *rt = CCRenderTexture::create(
textureSize.width, textureSize.height);
if(!rt)
{
//ERROR_MSG("create render texture failed !!!!");
addChild(m_label);
return;
}
m_label->setColor(m_fullColor);
ccBlendFunc originalBlend = m_label->getBlendFunc();
ccBlendFunc func = { GL_SRC_ALPHA, GL_ONE};
m_label->setBlendFunc(func);
m_label->setAnchorPoint(ccp(0.5, 0.5));
rt->begin();
for(int i = 0; i < 360; i += 15)
{
float r = CC_DEGREES_TO_RADIANS(i);
m_label->setPosition(ccp(
textureSize.width * 0.5f + sin(r) * strokeSize,
textureSize.height * 0.5f + cos(r) * strokeSize));
m_label->visit();
}
m_label->setColor(m_StrokeColor);
m_label->setBlendFunc(originalBlend);
m_label->setPosition(ccp(textureSize.width * 0.5f, textureSize.height * 0.5f));
m_label->visit();
rt->end();
CCTexture2D *texture = rt->getSprite()->getTexture();
texture->setAliasTexParameters();
m_sprite = CCSprite::createWithTexture(rt->getSprite()->getTexture());
setContentSize(m_sprite->getContentSize());
m_sprite->setAnchorPoint(ccp(0, 0));
m_sprite->setPosition(ccp(0, 0));
((CCSprite *)m_sprite)->setFlipY(true);
addChild(m_sprite);
}
使用:
CCLabelTTF* ttf = CCLabelTTF::create("ttf label","Arial",50);
CCStrokeLabel* ttfStroke = CCStrokeLabel::create(ttf,ccc3(255,0,0),ccc3(0,0,255),2.0f);
ttfStroke->setPosition(ccp(10,250));
this->addChild(ttfStroke,200);
CCBlink* blinkx = CCBlink::create(1.0,5);
ttfStroke->runAction(blinkx);
CCLabelTTF* ttfx = CCLabelTTF::create("ttf label test white","Arial",50);
CCStrokeLabel* ttfStrokeWhite = CCStrokeLabel::create(ttfx,ccc3(0,255,0),ccc3(255,255,255),4.0f);
ttfStrokeWhite->setPosition(ccp(10,100));
this->addChild(ttfStrokeWhite,200);
运行效果: