在项目中对于文字的显示要求会有很多种,比如加描边、加阴影、个别文字加颜色、字的行间距与列间距等等一些。。最近在看
cocos2d-x源码时发现引擎确实很强大里面有对文字的加描边与阴影的创建方法,但我在写实际运用时发现对文字加描边的效果不是很好,而且也没有找到可以设置字的行间距与列间距的方法,所以就写下了下面的方法以实现设置字的行间距与列间距,支持中英文状态下混输,支持自动换行。。有不好的地方请大家多多点评。。谢谢。。。。!.h文件
- #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文件
- #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%的上升。(不可重复)";
- CCLabelTTF*ttf=horizontalSpacingANDverticalSpacing(str,"Helvetica",26,10,300);
- ttf->setPosition(ccp(size.width*0.2,size.height*0.8));
- this->addChild(ttf);
- returntrue;
- }
- 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;
- }
- }
- 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;
- }
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|