ttf 字的水平距与垂直距

来源:互联网 发布:成都湛源软件 编辑:程序博客网 时间:2024/04/30 19:03

原文:http://blog.csdn.net/song_hui_xiang/article/details/18002261

 在项目中对于文字的显示要求会有很多种,比如加描边、加阴影、个别文字加颜色、字的行间距与列间距等等一些。。最近在看cocos2d-x源码时发现引擎确实很强大里面有对文字的加描边与阴影的创建方法,但我在写实际运用时发现对文字加描边的效果不是很好,而且也没有找到可以设置字的行间距与列间距的方法,所以就写下了下面的方法以实现设置字的行间距与列间距,支持中英文状态下混输,支持自动换行。。有不好的地方请大家多多点评。。谢谢。。。。!

.h文件
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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文件
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  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.             const char* englishStr =_string.substr(index,1).c_str();  
  46.             labelTTF_arr->addObject(CCLabelTTF::create(englishStr, fontName, fontSize));  
  47.             index+= 1;  
  48.         }  
  49.         else{  
  50.             const char* chineseStr =_string.substr(index,3).c_str();  
  51.             labelTTF_arr->addObject(CCLabelTTF::create(chineseStr, 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. }  

0 0
原创粉丝点击