废话不多少说,直接看代码才是王道,这么简单,没可能说看不懂的~~~~~~~~~~~~~~~
//FontStroke.h
#pragma once
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
enum FontSizeType
{
Type_1 = 1,
Type_2 = 2,
Type_3 = 3
};
class FontStroke
{
public:
static FontStroke* getInstance();
//---先把需要描边的字体的所以属性都设置好才使用描边函数,如果字体有变化,那么重新调用函数重新描边就可以了---//
//---------label 需要描边的字体指针, fontSize 需要描边的字体大小, type 描边大小, StrokeColor 描边的颜色----------------//
void setUILabelStroke(UILabel* label, int fontSize, FontSizeType type = Type_1, ccColor3B StrokeColor=ccc3(0,0,0));
void setCCLabelTTFStroke(CCLabelTTF* label, FontSizeType type = Type_1, ccColor3B StrokeColor=ccc3(0,0,0));
private:
FontStroke();
~FontStroke();
static FontStroke* instance;
};
#define FONTSTROKE FontStroke::getInstance()
//FontStroke.cpp
#include "FontStroke.h"
FontStroke* FontStroke::instance = NULL;
FontStroke::FontStroke()
{
}
FontStroke::~FontStroke()
{
}
FontStroke* FontStroke::getInstance()
{
if(instance == NULL)
{
instance = new FontStroke();
}
return instance;
}
void FontStroke::setUILabelStroke(UILabel* label, int fontSize, FontSizeType type, ccColor3B StrokeColor)
{
if(label == NULL)
return;
label->removeAllChildren();
int dir[4][2]={-type,0, type,0, 0,-type, 0,type};
for(int i=0; i<4; i++)
{
UILabel* label_0 = UILabel::create();
label_0->setAnchorPoint(label->getAnchorPoint());
label_0->setSize(label->getSize());
label_0->setFontSize(fontSize);
label_0->setText(label->getStringValue());
label->addChild(label_0);
label_0->setPosition(ccp(dir[i][0], dir[i][1]));
label_0->setColor(StrokeColor);
}
UILabel* label_0 = UILabel::create();
label_0->setAnchorPoint(label->getAnchorPoint());
label_0->setSize(label->getSize());
label_0->setFontSize(fontSize);
label_0->setText(label->getStringValue());
label->addChild(label_0);
label_0->setColor(label->getColor());
}
void FontStroke::setCCLabelTTFStroke(CCLabelTTF* label, FontSizeType type,ccColor3B StrokeColor)
{
if(label == NULL)
return;
label->removeAllChildren();
int dir[4][2]={-type,0, type,0, 0,-type, 0,type};
for(int i=0; i<4; i++)
{
CCLabelTTF* label_0 = CCLabelTTF::create(label->getString(),label->getFontName(),label->getFontSize());
label_0->setAnchorPoint(label->getAnchorPoint());
label->addChild(label_0);
label_0->setPosition(ccp(label->getContentSize().width/2+dir[i][0], label->getContentSize().height/2+dir[i][1]));
label_0->setColor(StrokeColor);
}
CCLabelTTF* label_0 = CCLabelTTF::create(label->getString(),label->getFontName(),label->getFontSize());
label_0->setAnchorPoint(label->getAnchorPoint());
label->addChild(label_0);
label_0->setColor(label->getColor());
label_0->setPosition(ccp(label->getContentSize().width/2, label->getContentSize().height/2));
}
使用方法:
UILabel* ll = dynamic_cast<UILabel*>(m_pLayer->getWidgetByName("Label_49"));
FONTSTROKE->setUILabelStroke(ll,32);
CCLabelTTF* ttf = CCLabelTTF::create("132456", "黑体", 30);
FONTSTROKE->setCCLabelTTFStroke(ttf);
欧耶!!!就这样结束鸟