原文链接: 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. 目前国际上所用云计算平台IaaS、PaaS、SaaS简介

    随着云计算这个概念越来越为人所熟知,企业对云计算的重视程度也在日趋加深.这不仅是一种潮流,更体现了一种需求——数字化.现代化.科技化的整体需求.如今市场上云计算的运营商更是风起云涌,服务种类更是丰富繁 ...

  2. WebApi~通过HttpClient来调用Web Api接口

    异步请求 ///<summary> /// HttpClient实现Post请求(异步) /// </summary> static async void dooPost() ...

  3. filter、map函数的区别

    def even(num): if num%2==0: return True return False lis = [1,2,3,4,5,6,7,8,9] res = filter(even,lis ...

  4. APP全局异常捕获,并保存本地文件

    public class CrashHandler implements Thread.UncaughtExceptionHandler { public static final String TA ...

  5. 如何在Java的Filter中注入Service???

    今天在做用户使用cookie自动登录的时候,发现在LoginFilter中读取到cookie以后要进行查询数据库然后进行用户名和密码的比对,查询数据库肯定要用到Service和Dao,一开始我以为在s ...

  6. JS中的动态合集与静态合集

    JS的动态合集 前言 DOM是JavaScript重要组成部分,在DOM中有三个特别的集合分别是NodeList(节点的集合),NamedNodeMap(元素属性的集合)和HTMLCollection ...

  7. Bootstrap(Web前端CSS框架)

    官方定义: Bootstrap is the most popular HTML, CSS, and JS framework for developing responsive, mobile fi ...

  8. poj3481(splay tree 入门题)

    平衡树都能做. // // main.cpp // splay // // Created by 陈加寿 on 16/3/25. // Copyright © 2016年 chenhuan001. A ...

  9. Android获取应用程序的信息

    1.获取应用程序的版本号: private String getAppVersionName() { String versionName = ""; try { PackageM ...

  10. F - 简单计算器(栈)

    F - 简单计算器 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descripti ...