修複cocos2dx的Label,WP8下不能換行的問題

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   檔案   for   art   

搞了幾個小時,這個頭疼的問題,我給出代碼吧。

 

找到 

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;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.