cocos2dx 利用CCLabelTTF设置字的水平间距与垂直间距

cocos2dx 利用CCLabelTTF设置字的水平间距与垂直间距,第1张

概述在项目中对于文字的显示要求会有很多种,比如加描边、加阴影、个别文字加颜色、字的行间距与列间距等等一些。。最近在看 cocos2d-x源码时发现引擎确实很强大里面有对文字的加描边与阴影的创建方法,但我在写实际运用时发现对文字加描边的效果不是很好,而且也没有找到可以设置字的行间距与列间距的方法,所以就写下了下面的方法以实现设置字的行间距与列间距,支持中英文状态下混输,支持自动换行。。有不好的地方请大家 在项目中对于文字的显示要求会有很多种,比如加描边、加阴影、个别文字加颜色、字的行间距与列间距等等一些。。最近在看 cocos2d-x源码时发现引擎确实很强大里面有对文字的加描边与阴影的创建方法,但我在写实际运用时发现对文字加描边的效果不是很好,而且也没有找到可以设置字的行间距与列间距的方法,所以就写下了下面的方法以实现设置字的行间距与列间距,支持中英文状态下混输,支持自动换行。。有不好的地方请大家多多点评。。谢谢。。。。!.h文件 [cpp]@H_502_13@ view plain copy #ifndef__HELLOWORLD_SCENE_H__ #define__HELLOWORLD_SCENE_H__ #include"cocos2d.h" usingnamespacecocos2d; usingnamespacestd; classHelloWorld:publiccocos2d::cclayer { public: virtualboolinit(); staticcocos2d::CCScene*scene(); cclabelTTF*horizontalSpacingANDverticalSpacing(string_string,constchar*Fontname,floatFontSize,floathorizontalSpacing,floatverticalSpacing,floatlinewidth); CREATE_FUNC(HelloWorld); }; #endif//__HELLOWORLD_SCENE_H__
.cpp文件 [cpp]@H_502_13@ view plain copy #include"HelloWorldScene.h" #include"SimpleAudioEngine.h" CCScene*HelloWorld::scene() { CCScene*scene=CCScene::create(); HelloWorld*layer=HelloWorld::create(); scene->addChild(layer); returnscene; } boolHelloWorld::init() { if(!cclayer::init()) { returnfalse; } CCSizesize=CCDirector::sharedDirector()->getWinSize(); stringstr="对敌人造成4,000的伤害、回合数延后一回合(上限10)当前战斗中,弱点属性伤害加成10%的上升。(不可重复)"; //水平间距与垂直间距都是10像素,每行宽为300像素。 cclabelTTF*ttf=horizontalSpacingANDverticalSpacing(str,"Helvetica",26,10,300); ttf->setposition(ccp(size.wIDth*0.2,size.height*0.8)); this->addChild(ttf); returntrue; } /* horizontalSpacing:水平间距 verticalSpacing:垂直间距 linewidth:一行的最大宽度 */ cclabelTTF*HelloWorld::horizontalSpacingANDverticalSpacing(string_string,floatlinewidth) { CCArray*labelTTF_arr=CCArray::create(); intindex=0; intindex_max=strlen(_string.c_str()); boolis_end=true; while(is_end){ if(_string[index]>=0&&_string[index]<=127){ stringenglishStr=_string.substr(index,1).c_str(); labelTTF_arr->addobject(cclabelTTF::create(englishStr.c_str(),Fontname,FontSize)); index+=1; } else{ stringchineseStr=_string.substr(index,3).c_str(); labelTTF_arr->addobject(cclabelTTF::create(chineseStr.c_str(),FontSize)); index+=3; } if(index>=index_max){ is_end=false; } } //以上步骤是根据ASCII码找出中英文字符,并创建成一个cclabelTTF对象存入labelTTF_arr数组中。 //下面创建的原理是在cclabelTTF对象上添加子对象cclabelTTF,以此组合成一句话,以左上角第一个字为锚点。。 cclabelTTF*returnTTF=(cclabelTTF*)labelTTF_arr->objectAtIndex(0); floatNowWIDth=returnTTF->getContentSize().wIDth; cclabelTTF*dangqiangTTF=returnTTF; cclabelTTF*lineBeginTTF=returnTTF; intarr_count=labelTTF_arr->count(); for(inti=1;i<arr_count;i++){ cclabelTTF*beforeTTF=(cclabelTTF*)labelTTF_arr->objectAtIndex(i); beforeTTF->setAnchorPoint(ccp(0,0.5)); NowWIDth+=beforeTTF->getContentSize().wIDth; if(NowWIDth>=linewidth){ NowWIDth=returnTTF->getContentSize().wIDth; dangqiangTTF=lineBeginTTF; beforeTTF->setposition(ccp(0,-dangqiangTTF->getContentSize().height*0.5-verticalSpacing)); lineBeginTTF=beforeTTF; }else{ beforeTTF->setposition(ccp(dangqiangTTF->getContentSize().wIDth+horizontalSpacing,dangqiangTTF->getContentSize().height*0.5)); } dangqiangTTF->addChild(beforeTTF); dangqiangTTF=beforeTTF; } returnreturnTTF; }
总结

以上是内存溢出为你收集整理的cocos2dx 利用CCLabelTTF设置字的水平间距与垂直间距全部内容,希望文章能够帮你解决cocos2dx 利用CCLabelTTF设置字的水平间距与垂直间距所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1070886.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存