Label setMaxLineWidth

Hi,
I’m trying to use setMaxLineWidth in a Label, but it doesn’t work with Label::createWithSystemFont
Examples:

It not works:

Label *label = Label::createWithSystemFont("Test1 Test1 Test1 Test1 Test1 Test1 Test1 Test1", "Arial", 12);
label->enableBold();
label->setColor(Color3B::ORANGE);
label->setMaxLineWidth((origin.x + visibleSize.width) * 0.20f);
label->setPosition(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2);
scene->addChild(label);

It works:

Label *label = Label::createWithTTF("Test1 Test1 Test1 Test1 Test1 Test1 Test1 Test1", "fonts/Riffic.ttf", 12);
label->setColor(Color3B::ORANGE);
label->setMaxLineWidth((origin.x + visibleSize.width) * 0.20f);
label->setPosition(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2);
scene->addChild(label);

It’s a bug? If not, what’s the solution? I need to use createWithSystemFont and setMaxLineWidth
Thanks.

// cc @slackmoehrle it’s a bug?

I think system font doesn’t allow customizations. You need to accept the defaults the system the app is run on provides? IIRC.

But how can I solve the problem? I need configure a limit text in width.
And I need use a generic font, because the text could be in multiple languages like chinese, spanish, english, russian, etc.

I use TTF fonts and change the size based upon a scale factor that I calculate.

Bitmap fonts are options too but you can’t change those. You need to include a font for each size you need in your game.

I don’t want to scale the text because it loses quality.

http://cocos2d-x.org/docs/cocos2d-x/en/ui_components/labels.html

SystemFont isn’t possible.

Hm… I would like to find a solution, anyone? another ideas?
I need use a generic font, because the text could be in multiple languages like chinese, spanish, english, russian, etc. And I need to use setMaxLineWidth

cocos2d is open source, so you can try to implement by self :wink:
After implementation and testing please send PR to cocos2d github

Bitmap fonts could change font size via the setBMFontSizeInternal API:

void Label::restoreFontSize()
{
    if(_currentLabelType == LabelType::TTF){
        auto ttfConfig = this->getTTFConfig();
        ttfConfig.fontSize = _originalFontSize;
        this->setTTFConfigInternal(ttfConfig);
    }else if(_currentLabelType == LabelType::BMFONT){
        this->setBMFontSizeInternal(_originalFontSize);
    }else if(_currentLabelType == LabelType::STRING_TEXTURE){
        this->setSystemFontSize(_originalFontSize);
    }
}

Though its actually just scaling the characters

Did you try cocos2d::Label::setOverflow?

But this isn’t safe. It will look horrrible under a lot of circumstances.

I found a solution :wink:
I tested setWidth instead of setMaxLineWidth and it works (don’t understand why, but it works).
Thanks to all.

Can you share code so I can test too?

Yes, of course.

Label *label = Label::createWithSystemFont("Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test", "Arial", 12);
label->setWidth((origin.x + visibleSize.width) * 0.20);
label->enableBold();
label->setColor(Color3B::ORANGE);
label->setPosition(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2);
this->addChild(label);