ttf 字的水平距与垂直距
来源:互联网 发布:成都湛源软件 编辑:程序博客网 时间:2024/04/30 19:03
原文:http://blog.csdn.net/song_hui_xiang/article/details/18002261
在项目中对于文字的显示要求会有很多种,比如加描边、加阴影、个别文字加颜色、字的行间距与列间距等等一些。。最近在看cocos2d-x源码时发现引擎确实很强大里面有对文字的加描边与阴影的创建方法,但我在写实际运用时发现对文字加描边的效果不是很好,而且也没有找到可以设置字的行间距与列间距的方法,所以就写下了下面的方法以实现设置字的行间距与列间距,支持中英文状态下混输,支持自动换行。。有不好的地方请大家多多点评。。谢谢。。。。!
.h文件- #ifndef __HELLOWORLD_SCENE_H__
- #define __HELLOWORLD_SCENE_H__
- #include "cocos2d.h"
- using namespace cocos2d;
- using namespace std;
- class HelloWorld : public cocos2d::CCLayer
- {
- public:
- virtual bool init();
- static cocos2d::CCScene* scene();
- CCLabelTTF* horizontalSpacingANDverticalSpacing(string _string, const char *fontName, float fontSize, float horizontalSpacing, float verticalSpacing, float lineWidth);
- 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);
- return scene;
- }
- bool HelloWorld::init()
- {
- if ( !CCLayer::init() )
- {
- return false;
- }
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- string str = "对敌人造成4,000的伤害、回合数延后一回合(上限10)当前战斗中,弱点属性伤害加成10%的上升。(不可重复)";
- //水平间距与垂直间距都是10像素,每行宽为300像素。
- CCLabelTTF* ttf = horizontalSpacingANDverticalSpacing(str, "Helvetica", 26, 10, 10, 300);
- ttf->setPosition(ccp(size.width*0.2, size.height*0.8));
- this->addChild(ttf);
- return true;
- }
- /*
- horizontalSpacing: 水平间距
- verticalSpacing: 垂直间距
- lineWidth: 一行的最大宽度
- */
- CCLabelTTF* HelloWorld::horizontalSpacingANDverticalSpacing(string _string, const char *fontName, float fontSize, float horizontalSpacing, float verticalSpacing, float lineWidth)
- {
- CCArray* labelTTF_arr = CCArray::create();
- int index = 0;
- int index_max = strlen(_string.c_str());
- bool is_end = true;
- while (is_end) {
- if (_string[index] >= 0 && _string[index] <= 127) {
- const char* englishStr =_string.substr(index,1).c_str();
- labelTTF_arr->addObject(CCLabelTTF::create(englishStr, fontName, fontSize));
- index+= 1;
- }
- else{
- const char* chineseStr =_string.substr(index,3).c_str();
- labelTTF_arr->addObject(CCLabelTTF::create(chineseStr, fontName, fontSize));
- index+= 3;
- }
- if (index>=index_max) {
- is_end=false;
- }
- }
- //以上步骤是根据ASCII码找出中英文字符,并创建成一个CCLabelTTF对象存入labelTTF_arr数组中。
- //下面创建的原理是在CCLabelTTF对象上添加子对象CCLabelTTF,以此组合成一句话,以左上角第一个字为锚点。。
- CCLabelTTF* returnTTF = (CCLabelTTF*)labelTTF_arr->objectAtIndex(0);
- float nowWidth = returnTTF->getContentSize().width;
- CCLabelTTF* dangqiangTTF = returnTTF;
- CCLabelTTF* lineBeginTTF = returnTTF;
- int arr_count = labelTTF_arr->count();
- for (int i=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;
- }
- return returnTTF;
- }
0 0
- ttf 字的水平距与垂直距
- 图像的水平与垂直积分投影
- 图像的水平与垂直积分投影
- android的布局水平与垂直显示
- 水平居中与垂直居中的区别
- 数据库的水平扩展与垂直扩展
- 水平居中与垂直居中的区别
- CSS的水平居中与垂直居中
- 数据库的水平拆分与垂直拆分
- C# Listbox的行间距以及文本水平垂直居中
- 多行文字的垂直水平居中
- 水平切分与垂直切分
- 图片水平与垂直居中
- div水平与垂直居中
- CSS垂直与水平居中
- 水平居中与垂直居中
- 垂直拆分与水平拆分
- css水平与垂直居中
- win7/8运行StartUML是报错解决方案
- Ubuntu安装JDK7
- leetcode: Remove Element
- 作业3a. 编写计算“两个整数的最大公约数”程序
- SQLServer char、varchar、nchar、nvarchar的区别
- ttf 字的水平距与垂直距
- RTEMS POSIX API 用户指南
- 解题笔记(21)——字符串的排列组合问题
- java 输入输出-课堂作业7-6
- MCP2515调试笔记
- cocos2dx中层的初始化CCLayer::init()
- 手动5:开机自启动
- Activity的用法(一):Notification Activity
- JQuery插件开发相关