原文链接: http://www.cnblogs.com/zouzf/p/3985330.html

在wp8平台上,CCLabeTTF显示中文不会自动换行,看了下源码,原来底层的实现是根据text的空格进行判断的,每遇到一个空格就判断是否超过label的宽度,超过就换行,但text如果是中文的话,哪来的空格给换行~~

以下实现全部参考 http://blog.csdn.net/hopingwhite/article/details/38414917 ,整理后的代码如下:

在CCFreeTypeFont.h里的CCFreeTypeFont类添加两个方法:

FT_Error addLine(const std::string& line);
 void endLine_chinese();

实现如下:

 void CCFreeTypeFont::endLine_chinese()
{
if(m_currentLine)
{
m_lines.push_back(m_currentLine);
compute_bbox(m_currentLine->glyphs, &m_currentLine->bbox);
m_currentLine->width = m_currentLine->bbox.xMax - m_currentLine->bbox.xMin;
m_textWidth = max(m_textWidth,m_currentLine->bbox.xMax - m_currentLine->bbox.xMin);
m_textHeight += m_lineHeight;
m_currentLine = NULL;
}
} FT_Error CCFreeTypeFont::addLine(const std::string& line)
{
wchar_t * pwszBuffer = nullptr; int num_chars = line.size();
int nBufLen = num_chars + ;
pwszBuffer = new wchar_t[nBufLen];
if (!pwszBuffer)
{
return -;
} memset(pwszBuffer, , nBufLen);
num_chars = MultiByteToWideChar(CP_UTF8, , line.c_str(), num_chars, pwszBuffer, nBufLen);
pwszBuffer[num_chars] = '\0'; int maxWidth = m_inWidth ? m_inWidth : m_windowWidth; newLine();
FT_Vector pen = m_currentLine->pen;
FT_GlyphSlot slot = m_face->glyph;
FT_UInt glyph_index;
FT_UInt previous = ;
FT_Error error = ;
unsigned int numGlyphs = ; FT_Bool useKerning = FT_HAS_KERNING(m_face); int n = ;
while (n < num_chars)
{
TGlyph glyph; /* convert character code to glyph index */
FT_ULong c = pwszBuffer[n++];
glyph_index = FT_Get_Char_Index(m_face, c); if (useKerning && previous && glyph_index)
{
FT_Vector delta;
FT_Get_Kerning(m_face, previous, glyph_index,
FT_KERNING_DEFAULT, &delta);
pen.x += delta.x >> ;
} /* store current pen position */
glyph.pos = pen;
glyph.index = glyph_index; /* load glyph image into the slot without rendering */
error = FT_Load_Glyph(m_face, glyph_index, FT_LOAD_DEFAULT);
if (error)
continue; /* ignore errors, jump to next glyph */ /* extract glyph image and store it in our table */
error = FT_Get_Glyph(m_face->glyph, &glyph.image);
if (error)
continue; /* ignore errors, jump to next glyph */ /* translate the glyph image now */
FT_Glyph_Transform(glyph.image, , &glyph.pos); /* increment pen position */
pen.x += slot->advance.x >> ; if (pen.x > maxWidth)
{
m_currentLine->pen = pen; // endLine();
endLine_chinese(); newLine();
pen = m_currentLine->pen;
previous = ;
n--;
}
else
{
m_currentLine->glyphs.push_back(glyph);
/* record current glyph index */
previous = glyph_index;
}
} if (m_currentLine)
{
m_currentLine->pen = pen; //endLine();
endLine_chinese();
} CC_SAFE_DELETE_ARRAY(pwszBuffer);
return error;
}

FT_Error CCFreeTypeFont::initGlyphs(const char* text)的实现改成如下:

 FT_Error CCFreeTypeFont::initGlyphs(const char* text)
{
FT_Error error = ;
std::stringstream stringStream(text);
std::string line;
vector<std::string> lines;
vector<std::string> words; m_textWidth = ;
m_textHeight = ;
// the height of a line of text based on the max height of a glyph in the font size
m_lineHeight = ((m_face->size->metrics.ascender) >> ) - ((m_face->size->metrics.descender) >> ); m_lines.clear(); while(std::getline(stringStream, line) && !error)
{
/* newLine(); std::size_t prev = 0, pos;
while ((pos = line.find_first_of(" ", prev)) != std::string::npos)
{
if (pos > prev)
{
addWord(line.substr(prev, pos-prev));
}
prev = pos + 1;
}
if (prev < line.length())
{
addWord(line.substr(prev, std::string::npos));
}
endLine();
*/
addLine(line);
} return error;
}

再次感谢: http://blog.csdn.net/hopingwhite/article/details/38414917

原文链接: http://www.cnblogs.com/zouzf/p/3985330.html

Cocos2d-x项目移植到WP8系列之八:CCLabelTTF显示中文不换行的更多相关文章

  1. Cocos2d-x项目移植到WP8系列之二:开篇

    原文链接: http://www.cnblogs.com/zouzf/p/3970130.html 开发环境一笔带过吧,主板和CPU要支持虚拟化技术,要开启才行,装个64位win8.1系统,win8不 ...

  2. Cocos2d-x项目移植到WP8系列之三:C++和C#的交互

    原文链接: http://www.cnblogs.com/zouzf/p/3971021.html 上一篇提到工程使用 XAML 和 Direct3D 项目模板 是因为要涉及到C++和C#的交互,微软 ...

  3. Cocos2d-x项目移植到WP8系列之一:前传

    原文链接: http://www.cnblogs.com/zouzf/p/3969993.html 许久没动笔了,随想一直都有动笔的想法,但拖来拖去,归根到底还是一个懒字吧 .发现人的惰性真是太强大了 ...

  4. Cocos2d-x项目移植到WP8系列之九:使用自定义shader

    本文原链接:http://www.cnblogs.com/zouzf/p/3995132.html 有时候想得到一些例如灰度图等特殊的渲染效果,就得用到自定义shader,关于shader的一些背景知 ...

  5. Cocos2d-x项目移植到WP8系列之六:C#工程使用C++的DLL

    原文链接: http://www.cnblogs.com/zouzf/p/3984510.html 此时,一些大问题都被解决后,整个工程基本能跑起来了,最后一个大问题是:业务层是用Lua开发的,底层的 ...

  6. Cocos2d-x项目移植到WP8系列之四:文件操作

    原文链接: http://www.cnblogs.com/zouzf/p/3972457.html 读写文件Cocos已经用fopen fwrite来做好了,这里说的主要是文件和文件夹的创建.删除.判 ...

  7. Cocos2d-x项目移植到WP8系列之七:中文显示乱码

    原文链接:http://www.cnblogs.com/zouzf/p/3984628.html C++和C#互调时经常会带一些参数过去例如最常见的字符串,如果字符串里有中文的话,会发现传递过去后变成 ...

  8. Cocos2d-x项目移植到WP8系列之五:播放MP3

    原文链接: http://www.cnblogs.com/zouzf/p/3972549.html 这一块的细节还是不太了解,只是东凑西拼能跑起来而已 1.网上下载lamb库 生成需要的lib库,详情 ...

  9. Cocos2d-x项目移植到WP8小记

    Cocos2d-x项目移植到WP8小记 作者: K.C. 日期: 10/24/2013 Date: 2013-10-24 00:33 Title: Cocos2d-x项目移植到WP8小记 Tags: ...

随机推荐

  1. Codeforces Round #404 (Div. 2) DE

    昨晚玩游戏竟然不小心错过了CF..我是有多浪啊. 今天总算趁着下课时间补了,感觉最后两题还是挺有意思的,写个题解. D: 题目大意: 给出一个括号序列,问有多少个子序列 是k个'(' + k个')' ...

  2. ios --也是在B页面的生命周期设置如下代码。方法一是直接关闭和激活侧滑手势,方法二则是B遵循协议UIGestureRecognizerDelegate,设置侧滑交互代理,重写手势方法。

    @property (weak, nonatomic) id<UIGestureRecognizerDelegate> restoreInteractivePopGestureDelega ...

  3. FinalShell Mac OS版,Linux版安装及教程

    该版本功能和windows版基本一样,但是主机检测和远程桌面功能由于兼容性问题暂时无法使用,以后会支持. 该版本功能和windows版基本一样,但是主机检测和远程桌面功能由于兼容性问题暂时无法使用,以 ...

  4. Laravel5.1 -控制器(初步了解)

    首先道个歉 这篇笔记是前两天就应该写的,可大K有点事儿要忙 就耽误了,今天抽空学了学控制器,并写个笔记分享下. 为什么要使用控制器 像我们之前写一些逻辑呢都是在Route(路由)中,搞得Route文件 ...

  5. Android错误——基础篇

    1. Android工程在真机上运行调试: 花了二个小时的时间来把App热部署到小米机上,简直让我寒透了心, 原本是按照网上提供的步骤一步步的做着,没想到小米神机居然出的是什么内测小米助手,两个窗口来 ...

  6. SharePoint服务器端对象模型 之 访问文件和文件夹(Part 4)

    (四)列表附件 列表的附件也是文件系统的一部分,它依附于普通列表的列表条目之上(文档库没有附件),它的操作在一些地方和文档库中文档的操作非常类似.   1.附件的读取 一个列表条目的附件可以使用SPL ...

  7. Velocity模板引擎介绍

    整理下Velocity使用方法,整理比较详细用例 1 Velocity基础语法 1.1 用户和开发人员参考文档 http://velocity.apache.org/engine/releases/v ...

  8. Django 路由系统(URL)

    介绍 Django 1.11版本 URLConf官方文档 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表. 你就是以这种方式告 ...

  9. 0x07 MySQL 多表查询

    Some Content From——Egon's Blog http://www.cnblogs.com/linhaifeng/articles/7126847.html 一 准备表 准备表 #建表 ...

  10. app开发多少钱一个

    经常听网友问app开发要多少钱,这个问题太宽泛了,需要根据具体的需求才好定价,也就是要先做好需求分析(前面我们写了一个app开发需求文档模板),不同的功能不同的价位,就像我们买电脑,cpu多少钱.主板 ...