Projects > cpp > Issues > Bug #1430

We are migrating issue tracker of Cocos2d-x Project to Github, please create new issue there. Thanks.

Create Issue on Github

Can not compute correct label's width or height when its value is 0

Bug #1430 [Closed]
zhangxm 2012-08-15 10:15 . Updated over 4 years ago

sample codes

CCLabelTTF* label = CCLabelTTF::create("abcdefghijklmnopqrstuvwxyz", 
                                                                CCSizeMake(320, 200),
                                                                kCCTextAlignmentCenter,
                                                                kCCVerticalTextAlignmentTop,
                                                                "Paint Boy",  
                                                                 32);

label->setDimentions(CCSizeMake(500,0));
label->setPosition(ccp(label->getContentSize().width / 2, label->getContentSize().height / 2));

The “label” will only displayed in one line, not convert to more lines according to the width 500.

The reason is that CCLabelTTF::updateTexture will use (0,0) as dimensions when width or height is 0.

void CCLabelTTF::updateTexture()
{
    CCTexture2D *tex;
    if (m_tDimensions.width == 0 || m_tDimensions.height == 0)
    {
        tex = new CCTexture2D();
        tex->initWithString(m_string.c_str(), m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR()) ;
    }
    else
    {
        tex = new CCTexture2D();
        tex->initWithString(m_string.c_str(),
                            CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), 
                            m_hAlignment,
                            m_vAlignment,
                            m_pFontName->c_str(),
                            m_fFontSize * CC_CONTENT_SCALE_FACTOR());
    }
 ...
}

So we should only invoke

tex->initWithString(m_string.c_str(),
                            CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), 
                            m_hAlignment,
                            m_vAlignment,
                            m_pFontName->c_str(),
                            m_fFontSize * CC_CONTENT_SCALE_FACTOR());

no matter what the value of dimension is.

zhangxm 2012-08-16 04:08
  • Status changed from New to Resolved
  • % Done changed from 0 to 100

Applied in changeset commit:45cb171dcdb70639fcedc03b5e6728db4f90ae25.

zhangxm 2012-08-16 04:08

Applied in changeset commit:a603105e941f2af23a5e4de5a4f677109d79d3dd.

zhangxm 2012-08-20 02:49
  • Status changed from Resolved to Closed

Atom PDF

Status:Closed
Start date:2012-08-15
Priority:Normal
Due date:
Assignee:zhangxm
% Done:

100%

Category:all
Target version:cocos2d-2.0-x-2.0.2

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.