cocos2dx 利用CCLabelTTF設置字的水平間距與垂直間距

在項目中對於文字的顯示要求會有很多種,比如加描邊、加陰影、個別文字加顏色、字的行間距與列間距等等一些。。最近在看cocos2d-x源碼時發現引擎確實很強大里面有對文字的加描邊與陰影的創建方法,但我在寫實際運用時發現對文字加描邊的效果不是很好,而且也沒有找到可以設置字的行間距與列間距的方法,所以就寫下了下面的方法以實現設置字的行間距與列間距,支持中英文狀態下混輸,支持自動換行。。有不好的地方請大家多多點評。。謝謝。。。。!.h文件
  1. #ifndef __HELLOWORLD_SCENE_H__  
  2. #define __HELLOWORLD_SCENE_H__  
  3.   
  4. #include "cocos2d.h"  
  5.   
  6. using namespace cocos2d;  
  7. using namespace std;  
  8.   
  9. class HelloWorld : public cocos2d::CCLayer  
  10. {  
  11. public:  
  12.     virtual bool init();  
  13.   
  14.     static cocos2d::CCScene* scene();  
  15.       
  16.     CCLabelTTF* horizontalSpacingANDverticalSpacing(string _string, const char *fontName, float fontSize, float horizontalSpacing, float verticalSpacing, float lineWidth);  
  17.   
  18.     CREATE_FUNC(HelloWorld);  
  19. };  
  20.   
  21. #endif // __HELLOWORLD_SCENE_H__  

.cpp文件
  1. #include "HelloWorldScene.h"  
  2. #include "SimpleAudioEngine.h"  
  3.   
  4.   
  5. CCScene* HelloWorld::scene()  
  6. {  
  7.     CCScene *scene = CCScene::create();  
  8.     HelloWorld *layer = HelloWorld::create();  
  9.     scene->addChild(layer);  
  10.     return scene;  
  11. }  
  12.   
  13. bool HelloWorld::init()  
  14. {  
  15.     if ( !CCLayer::init() )  
  16.     {  
  17.         return false;  
  18.     }  
  19.       
  20.     CCSize size = CCDirector::sharedDirector()->getWinSize();  
  21.       
  22.     string str = "對敵人造成4,000的傷害、回合數延後一回合(上限10)當前戰鬥中,弱點屬性傷害加成10%的上升。(不可重複)";  
  23.     //水平間距與垂直間距都是10像素,每行寬爲300像素。  
  24.     CCLabelTTF* ttf = horizontalSpacingANDverticalSpacing(str, "Helvetica", 26, 10, 10, 300);  
  25.     ttf->setPosition(ccp(size.width*0.2, size.height*0.8));  
  26.     this->addChild(ttf);  
  27.       
  28.     return true;  
  29. }  
  30.   
  31.   
  32. /* 
  33.  horizontalSpacing: 水平間距 
  34.  verticalSpacing:   垂直間距 
  35.  lineWidth:         一行的最大寬度 
  36.  */  
  37. CCLabelTTF* HelloWorld::horizontalSpacingANDverticalSpacing(string _string, const char *fontName, float fontSize, float horizontalSpacing, float verticalSpacing, float lineWidth)  
  38. {  
  39.     CCArray* labelTTF_arr = CCArray::create();  
  40.     int index = 0;  
  41.     int index_max = strlen(_string.c_str());  
  42.     bool is_end = true;  
  43.     while (is_end) {  
  44.         if (_string[index] >= 0 && _string[index] <= 127) {  
  45.             string englishStr =_string.substr(index,1).c_str();  
  46.             labelTTF_arr->addObject(CCLabelTTF::create(englishStr.c_str(), fontName, fontSize));  
  47.             index+= 1;  
  48.         }  
  49.         else{  
  50.             string chineseStr =_string.substr(index,3).c_str();  
  51.             labelTTF_arr->addObject(CCLabelTTF::create(chineseStr.c_str(), fontName, fontSize));  
  52.             index+= 3;  
  53.         }  
  54.         if (index>=index_max) {  
  55.             is_end=false;  
  56.         }  
  57.     }  
  58.     //以上步驟是根據ASCII碼找出中英文字符,並創建成一個CCLabelTTF對象存入labelTTF_arr數組中。  
  59.       
  60.       
  61.     //下面創建的原理是在CCLabelTTF對象上添加子對象CCLabelTTF,以此組合成一句話,以左上角第一個字爲錨點。。  
  62.     CCLabelTTF* returnTTF = (CCLabelTTF*)labelTTF_arr->objectAtIndex(0);  
  63.     float nowWidth = returnTTF->getContentSize().width;  
  64.     CCLabelTTF* dangqiangTTF = returnTTF;  
  65.     CCLabelTTF* lineBeginTTF = returnTTF;  
  66.       
  67.     int arr_count = labelTTF_arr->count();  
  68.     for (int i=1; i < arr_count; i++) {  
  69.         CCLabelTTF* beforeTTF = (CCLabelTTF*)labelTTF_arr->objectAtIndex(i);  
  70.         beforeTTF->setAnchorPoint(ccp(0, 0.5));  
  71.         nowWidth+=beforeTTF->getContentSize().width;  
  72.         if (nowWidth >= lineWidth) {  
  73.             nowWidth = returnTTF->getContentSize().width;  
  74.             dangqiangTTF = lineBeginTTF;  
  75.             beforeTTF->setPosition(ccp(0, -dangqiangTTF->getContentSize().height*0.5-verticalSpacing));  
  76.             lineBeginTTF = beforeTTF;  
  77.         }else{  
  78.             beforeTTF->setPosition(ccp(dangqiangTTF->getContentSize().width+horizontalSpacing, dangqiangTTF->getContentSize().height*0.5));  
  79.         }  
  80.         dangqiangTTF->addChild(beforeTTF);  
  81.         dangqiangTTF = beforeTTF;  
  82.     }  
  83.       
  84.     return returnTTF;  
  85. }  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章