cocos2dx 字体颜色判断TTF字体库是否有不支持的字体

cocos2d-js ttf字体总结(史上最详细教程)。
网上有关cocos2d引擎字体使用的教程可以说是满天飞我就不说了,本文主要讲解如何使用第三方.ttf字体。
cocos2d-js 分web和jsb版本,首先讲解web版本如何使用第三方xxxx.ttf字库,需要在resource中加载字体,格式如下:
var g_resource = [
....此处略去其它
type:&font&,
name:&Arial Bold&,
srcs:[&resource/font/arialbd.eot&, &resource/font/arialbd.ttf&]
//arialbd.eot, arialbd.ttf 这个名字arialbd随便起没关系(这是相对于web版本而言, //但是name一定要是字体库的名字)
之后在使用时直接用&Arial Bold&字体名字即可,
例如: var labTest = new cc.LabelTTF(&12345&, &Arial Bold&, 25);
现在讲jsb如何使用:在jsb中就不需要这么麻烦了,可以直接使用: 例如
var labTest = new cc.LabelTTF(&12345&, &resource/font/Arial Bold.ttf&, 25);
但是这里需要注意Arial Bold.ttf字体的名字,这个名字一定要是字体库的名字。特别和web版本的
arialbd.ttf区分开来。参考此文,http://www.cocos2dres.com/post/87.html
在cocosbuilder里使用时有几个注意事项
1、中文保存时选择unicode
2、导出时选择 text
3、打开导出的fnt文件,把info face="&I&&E&&N&A&&U" ,改为info face=""
4、CCLabelBMFont调用setString无效时,再调用setColor一下就好了。(具体原因未知)&
阅读(...) 评论()Cocos2dx中文支持问题的解决办法_百度文库
您的浏览器Javascript被禁用,需开启后体验完整功能,
享专业文档下载特权
&赠共享文档下载特权
&10W篇文档免费专享
&每天抽奖多种福利
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
Cocos2dx中文支持问题的解决办法
&&Cocos2dx中文支持问题的解决办法,本文介绍了两种方法,均亲测可用。
阅读已结束,下载本文需要
想免费下载更多文档?
定制HR最喜欢的简历
下载文档到电脑,同时保存到云知识,更方便管理
加入VIP
还剩7页未读,
定制HR最喜欢的简历
你可能喜欢CCLabelAtlas字体与CCLabelTTF字体的使用方法
时间: 20:59:57
&&&& 阅读:283
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&&&&&&&
1.26、字体
1、CCLabelAtlas字体
&&示例代码:
&&&&CCLabelAtlas*LBsocre4&=&CCLabelAtlas::create(CCString::createWithFormat(&%d&,&c)-&getCString(),&&labelatlasimg.png&,&24,&32,‘0‘);
&&&&&&&&&&&&&&&&LBsocre4-&setAnchorPoint(ccp(0,0.5));
&&&&&&&&&&&&&&&&LBsocre4-&setPosition(ccp(200,200));
& &&&&&&&&&&&&&LBsocre4-&setScale(0.7f);
&&&&&&&&&&&&&&&& &LBsocre4-&setString(CCString::createWithFormat(&%d&,&c)-&getCString());
& &&&&&&&&&&&&&this-&addChild(LBsocre4,5);
2、CCLabelTTF字体
&&&示例代码:
&&&&&CCLabelTTF*scoreLab&=&CCLabelTTF::create(&0&,&&Arial&,&35);
&&&&&&&&&&&&&&&scoreLab-&setPosition(ccp(a,b));
&&&& &&&&&&&&&&scoreLab-&setAnchorPoint(ccp(0,&1));
&&&&&&&&&&&&&&&scoreLab-&setColor(ccc3(255,255,0));
&&&& &&&&&&&&&&scoreLab-&setString(CCString::createWithFormat(&%d&,&c)-&getCString());
&&&&&&&&&&&&&&&&this-&addChild(scoreLab,&2);
标签:&&&&&&&&&&&&&&&原文:http://blog.csdn.net/a/article/details/
教程昨日排行
&&国之画&&&& &&&&&&
&& &&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!cocos2d实现CCLabelTTF真正字体描边效果
在开发游戏中,我们需要在需要在游戏中显示一个字体轮廓比较清晰的效果,我们就需要给字体的周围进行描边,让字体显示比较更加突出,我重写了cclabelttf类,使它具有描边的特效,和描边的大小以及颜色。。。
[cpp]& #include &cocos2d.h&&&
using namespace cocos2d;&
class StrokeLabelTTF : public cocos2d::CCNode&
&&& StrokeLabelTTF();&
&&& ~StrokeLabelTTF();&
&&& static StrokeLabelTTF* create(const char *string, const char *fontName, float fontSize, float strokeSize,const cocos2d::ccColor3B&&&&&& colStroke = cocos2d::ccc3(0, 0, 0), cocos2d::CCTextAlignment hAlignment=cocos2d::kCCTextAlignmentCenter, cocos2d::CCVerticalTextAlignment vAlignment=cocos2d::kCCVerticalTextAlignmentTop);&
&&& bool initWithString(const char *label, const char *fontName, float fontSize, float fStrokeSize, const cocos2d::ccColor3B&&&&&& colStroke, cocos2d::CCTextAlignment hAlignment, cocos2d::CCVerticalTextAlignment vAlignment);&
&&& static StrokeLabelTTF * create(const char *string, const char *fontName, float fontSize);&
&&& void setColor(const cocos2d::ccColor3B& color3);&
&&& void setString(const char *label);&
&&& void setStrokeColor(cocos2d::ccColor3B col){ m_colStroke = updateStroke(); }&
&&& void setStrokeSize(float StrokeSize){ m_fStrokeSize = StrokeS updateStroke();}&
&&& void setAnchorPoint(const cocos2d::CCPoint &anchorPoint);&
protected:&
&&& void updateStroke();&
&&& float&&&&&&&&&&&&&&&&&& m_fStrokeS&
&&& cocos2d::ccColor3B&&&&& m_colS&
&&& cocos2d::CCSprite*&&&&& m_&
&&& cocos2d::CCLabelTTF*&&& m_&
&&& CCPoint anchorP&
#include &cocos2d.h&
using namespace cocos2d;
class StrokeLabelTTF : public cocos2d::CCNode
&StrokeLabelTTF();
&~StrokeLabelTTF();
&static StrokeLabelTTF* create(const char *string, const char *fontName, float fontSize, float strokeSize,const cocos2d::ccColor3B&&&&&& colStroke = cocos2d::ccc3(0, 0, 0), cocos2d::CCTextAlignment hAlignment=cocos2d::kCCTextAlignmentCenter, cocos2d::CCVerticalTextAlignment vAlignment=cocos2d::kCCVerticalTextAlignmentTop);
&bool initWithString(const char *label, const char *fontName, float fontSize, float fStrokeSize, const cocos2d::ccColor3B&&&&&& colStroke, cocos2d::CCTextAlignment hAlignment, cocos2d::CCVerticalTextAlignment vAlignment);
&&& static StrokeLabelTTF * create(const char *string, const char *fontName, float fontSize);
&void setColor(const cocos2d::ccColor3B& color3);
&void setString(const char *label);
&void setStrokeColor(cocos2d::ccColor3B col){ m_colStroke = updateStroke(); }
&void setStrokeSize(float StrokeSize){ m_fStrokeSize = StrokeS updateStroke();}
&&& void setAnchorPoint(const cocos2d::CCPoint &anchorPoint);
protected:
&void updateStroke();
&float&&&&&&&&&&&&&&&&&& m_fStrokeS
&cocos2d::ccColor3B&&&&& m_colS
&cocos2d::CCSprite*&&&&& m_
&cocos2d::CCLabelTTF*&&& m_
&&& CCPoint anchorP
#include &StrokeLabelTTF.h&&&
USING_NS_CC;&
StrokeLabelTTF::StrokeLabelTTF()&
&&& :m_colStroke(ccc3(0,0,0))&
&&& ,m_fStrokeSize(1.0f)&
&&& ,m_sprite(NULL)&
&&& ,m_label(NULL)&
&&& ,anchorPoint(0.5,0.5)&
StrokeLabelTTF::~StrokeLabelTTF()&
&&& CC_SAFE_RELEASE_NULL(m_label);&
bool StrokeLabelTTF::initWithString(const char *string, const char *fontName, float fontSize, float strokeSize,const cocos2d::ccColor3B& colStroke, CCTextAlignment hAlignment,CCVerticalTextAlignment vAlignment)&
&&& m_fStrokeSize = strokeS&
&&& m_colStroke = colS&
&&& m_label = CCLabelTTF::create(string, fontName, fontSize, CCSizeZero,hAlignment,vAlignment);&
&&& m_label-&retain();&
&&& updateStroke();&
StrokeLabelTTF* StrokeLabelTTF::create(const char *string, const char *fontName, float fontSize, float fStrokeSize,const cocos2d::ccColor3B& colStroke ,CCTextAlignment hAlignment,CCVerticalTextAlignment vAlignment)&
&&& StrokeLabelTTF *pRet = new StrokeLabelTTF();&
&&& if(pRet && pRet-&initWithString(string, fontName, fontSize, fStrokeSize, colStroke, hAlignment, vAlignment))&
&&&&&&& pRet-&autorelease();&
&&&&&&& return pR&
&&& CC_SAFE_DELETE(pRet);&
&&& return NULL;&
StrokeLabelTTF * StrokeLabelTTF::create(const char *string, const char *fontName, float fontSize)&
&&& StrokeLabelTTF *pRet = new StrokeLabelTTF();&
&&& if(pRet && pRet-&initWithString(string,fontName,fontSize,1.5,ccc3(0,0,0),kCCTextAlignmentCenter,kCCVerticalTextAlignmentTop))&
&&&&&&& pRet-&autorelease();&
&&&&&&& return pR&
&&& CC_SAFE_DELETE(pRet);&
&&& return NULL;&
void StrokeLabelTTF::updateStroke()&
&&& if (m_sprite)&
&&&&&&& removeChild(m_sprite, true);&
&&& CCSize textureSize = m_label-&getContentSize();&
&&& textureSize.width += 2 * m_fStrokeS&
&&& textureSize.height += 2 * m_fStrokeS&
&&& //call to clear error&&
&&& glGetError();&
&&& CCRenderTexture *rt = CCRenderTexture::create(textureSize.width, textureSize.height);&
&&& if(!rt)&
&&&&&&& CCLOG(&create render texture failed !!!!&);&
&&&&&&& addChild(m_label);&
&&& ccColor3B col = m_label-&getColor();&
&&& m_label-&setColor(m_colStroke);&
&&& ccBlendFunc originalBlend = m_label-&getBlendFunc();&
&&& ccBlendFunc func = { GL_SRC_ALPHA, GL_ONE};&
&&& m_label-&setBlendFunc(func);&
&&& m_label-&setAnchorPoint(ccp(0.5f,0.5f));&
&&& 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) * m_fStrokeSize,&
&&&&&&&&&&& textureSize.height * 0.5f + cos(r) * m_fStrokeSize));&
&&&&&&& m_label-&visit();&
&&& m_label-&setColor(col);&
&&& 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(this-&anchorPoint);//------------&&
&&& m_sprite-&setPosition(ccp(0, 0));&
&&& ((CCSprite *)m_sprite)-&setFlipY(true);&
&&& addChild(m_sprite);&
void StrokeLabelTTF::setString(const char *label)&
&&& if (m_label)&
&&&&&&& if(0!=strcmp(label, m_label-&getString()))&
&&&&&&& {&
&&&&&&&&&&& m_label-&setString(label);&
&&&&&&&&&&& updateStroke();&
&&&&&&& }&
&&&&&&& CCLOG(&ERROR:CCLabelTTFStroke::setString m_label=NULL&);&
void StrokeLabelTTF::setColor(const ccColor3B& color3)&
&&& if (m_label)&
&&&&&&& ccColor3B col = m_label-&getColor();&
&&&&&&& if(color3.r!=col.r || color3.g!=col.g || color3.b!=col.b)&
&&&&&&& {&
&&&&&&&&&&& m_label-&setColor(color3);&
&&&&&&&&&&& updateStroke();&
&&&&&&& }&
&&&&&&& CCLOG(&ERROR:CCLabelTTFStroke::setColor m_label=NULL&);&
void StrokeLabelTTF::setAnchorPoint(const cocos2d::CCPoint &anchorPoint1)&
&&& this-&anchorPoint=anchorPoint1;&
&&& updateStroke();&
#include &StrokeLabelTTF.h&
USING_NS_CC;
StrokeLabelTTF::StrokeLabelTTF()
&:m_colStroke(ccc3(0,0,0))
&,m_fStrokeSize(1.0f)
&,m_sprite(NULL)
&,m_label(NULL)
&&& ,anchorPoint(0.5,0.5)
StrokeLabelTTF::~StrokeLabelTTF()
&CC_SAFE_RELEASE_NULL(m_label);
bool StrokeLabelTTF::initWithString(const char *string, const char *fontName, float fontSize, float strokeSize,const cocos2d::ccColor3B& colStroke, CCTextAlignment hAlignment,CCVerticalTextAlignment vAlignment)
&m_fStrokeSize = strokeS
&m_colStroke = colS
&m_label = CCLabelTTF::create(string, fontName, fontSize, CCSizeZero,hAlignment,vAlignment);
&m_label-&retain();
&updateStroke();
StrokeLabelTTF* StrokeLabelTTF::create(const char *string, const char *fontName, float fontSize, float fStrokeSize,const cocos2d::ccColor3B& colStroke ,CCTextAlignment hAlignment,CCVerticalTextAlignment vAlignment)
&StrokeLabelTTF *pRet = new StrokeLabelTTF();
&if(pRet && pRet-&initWithString(string, fontName, fontSize, fStrokeSize, colStroke, hAlignment, vAlignment))
&&pRet-&autorelease();
&&return pR
&CC_SAFE_DELETE(pRet);
&return NULL;
StrokeLabelTTF * StrokeLabelTTF::create(const char *string, const char *fontName, float fontSize)
&&& StrokeLabelTTF *pRet = new StrokeLabelTTF();
&&& if(pRet && pRet-&initWithString(string,fontName,fontSize,1.5,ccc3(0,0,0),kCCTextAlignmentCenter,kCCVerticalTextAlignmentTop))
&&&&&&& pRet-&autorelease();
&&&&&&& return pR
&&& CC_SAFE_DELETE(pRet);
&&& return NULL;
void StrokeLabelTTF::updateStroke()
&if (m_sprite)
&&removeChild(m_sprite, true);
&CCSize textureSize = m_label-&getContentSize();
&textureSize.width += 2 * m_fStrokeS
&textureSize.height += 2 * m_fStrokeS
&//call to clear error
&glGetError();
&CCRenderTexture *rt = CCRenderTexture::create(textureSize.width, textureSize.height);
&&CCLOG(&create render texture failed !!!!&);
&&addChild(m_label);
&ccColor3B col = m_label-&getColor();
&m_label-&setColor(m_colStroke);
&ccBlendFunc originalBlend = m_label-&getBlendFunc();
&ccBlendFunc func = { GL_SRC_ALPHA, GL_ONE};
&m_label-&setBlendFunc(func);
&m_label-&setAnchorPoint(ccp(0.5f,0.5f));
&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) * m_fStrokeSize,
&&&textureSize.height * 0.5f + cos(r) * m_fStrokeSize));
&&m_label-&visit();
&m_label-&setColor(col);
&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(this-&anchorPoint);//------------
&m_sprite-&setPosition(ccp(0, 0));
&((CCSprite *)m_sprite)-&setFlipY(true);
&addChild(m_sprite);
void StrokeLabelTTF::setString(const char *label)
&if (m_label)
&&if(0!=strcmp(label, m_label-&getString()))
&&&m_label-&setString(label);
&&&updateStroke();
&&CCLOG(&ERROR:CCLabelTTFStroke::setString m_label=NULL&);
void StrokeLabelTTF::setColor(const ccColor3B& color3)
&if (m_label)
&&ccColor3B col = m_label-&getColor();
&&if(color3.r!=col.r || color3.g!=col.g || color3.b!=col.b)
&&&m_label-&setColor(color3);
&&&updateStroke();
&&CCLOG(&ERROR:CCLabelTTFStroke::setColor m_label=NULL&);
void StrokeLabelTTF::setAnchorPoint(const cocos2d::CCPoint &anchorPoint1)
&&& this-&anchorPoint=anchorPoint1;
&&& updateStroke();

我要回帖

更多关于 cocos2dx 字体 的文章

 

随机推荐