首页 > 代码库 > 修复cocos2dx的Label,WP8下不能换行的问题

修复cocos2dx的Label,WP8下不能换行的问题

搞了几个小时,这个头疼的问题,我给出代码吧。

 

找到 

libcocos2d/platform/winrt/CCFreeTypeFont.cpp

(其中libcocos2d是项目名)然后将其中的函数 addWord 替换为我提供的即可。实在找不到文件的同学直接搜索吧。

需要注意的是我只简单处理了一下,所以中文下只支持UTF8字符串,非UTF8字符串会出问题。当然英文环境下任然是用默认逻辑。

我的cocos2dx版本是 3.2,如果你的版本不是这个,存在个别差异,那就自己改改吧。

希望大家拿走代码的时候在下面评论一下,好提高我分享的积极性,你懂得。

FT_Error CCFreeTypeFont::addWord(const std::string& word) {    std::vector<TGlyph> glyphs; // glyphs for the word    FT_BBox             bbox;   // bounding box containing all of the glyphs in the word    int maxWidth = m_inWidth ? m_inWidth : m_windowWidth;    std::string newWord;    if(m_currentLine->width > 0)     {        newWord =   + word;    }     else    {        newWord = word;    }    FT_Error error = initWordGlyphs(glyphs, newWord, m_currentLine->pen);    if(!error)     {        compute_bbox(glyphs, &bbox);        /*判断添加进去后整个line是否显示宽度大于设定宽度,是的话进行截取*/        if (Application::getInstance()->getCurrentLanguage() == LanguageType::CHINESE && bbox.xMax > maxWidth)        {            std:size_t start = 0, end = word.length();            while (true)            {                while (true)//这个字符比最宽还要宽,则进行截取                {                    end--;                    FT_BBox validBBox = bbox;                    std::vector<TGlyph> validGlyphs;                    FTLineInfo validLine = FTLineInfo();                    validLine.width = 0;                    validLine.pen.x = 0;                    validLine.pen.y = 0;                    //对UTF8字符进行切割                    if (end != word.length())                    {                        while (true)                        {                            unsigned char utf8charpart = word.at(end - 1);                            if ((utf8charpart & 0x80) != 0 &&                                (utf8charpart & 0xe0) != 0xc0 &&                                (utf8charpart & 0xf0) != 0xe0 &&                                (utf8charpart & 0xf8) != 0xf0)                            {                                end--;                            }                            else                            {                                end--;                                break;                            }                        }                    }                    std::string validStr = word.substr(start, end - start);                    FT_Error validError = initWordGlyphs(validGlyphs, validStr, validLine.pen);                    if (validError) break;                    compute_bbox(validGlyphs, &validBBox);                    if (validBBox.xMax <= maxWidth)                    {                        m_currentLine->glyphs.insert(validLine.glyphs.end(), validGlyphs.begin(), validGlyphs.end());                        if (m_currentLine->width == 0)                        {                            m_currentLine->bbox = validBBox;                        }                        else                        {                            m_currentLine->bbox.xMax = validBBox.xMax;                        }                        break;                    }                }                start = end;                end = word.length() + 1;                if (start == end - 1) break;                bbox = FT_BBox();                endLine();                newLine();            }        }        else        {            if (m_currentLine->width == 0 || bbox.xMax <= maxWidth)            {                m_currentLine->glyphs.insert(m_currentLine->glyphs.end(), glyphs.begin(), glyphs.end());                if (m_currentLine->width == 0)                {                    m_currentLine->bbox = bbox;                }                else                {                    m_currentLine->bbox.xMax = bbox.xMax;                }                m_currentLine->width = m_currentLine->bbox.xMax - m_currentLine->bbox.xMin;            }            else            {                endLine();                newLine();                addWord(word);            }        }    }    return error;}