Cocos2d::label fully justified

Cocos2d::label fully justified

What version of Cocos2d-x are you using?

I want create a label fully justified

On cocos2d ObjectiveC I use NSMutableParagraphStyle with alignement NSTextAlignmentJustified and when I create my label I use CCLabelTTF labelWithAttributedString…

He dont have equivalent on cocos2d-x… someone have already do this or can help ?



I would say you need to make this yourself. It’s not really hard to do. You can easily figure out the number of words and then the math to spread it.

I use the version 3.15

can you help me ?

is what I have done… but I asked if someone have already done :slight_smile:
It can be a good add-on on the next release.


Could you share your code please? :grinning:

Hi, everyone, we are developing on game and we need exactly this thing, but we are still new to all this, so can anyone tell how can we done it, any example or sample @slackmoehrle @sevent7 @NMCarl

tanks in advance :slight_smile:

To full justify a label?

  1. get the string size
  2. decide a font size
  3. size your label so that the string fits in it.
  4. adjust the kerning between characters to fit if needed.

thanks for reply @slackmoehrle i have done all first three part , can you explain me how can do forth one, thanks for reply :slight_smile:

kerning is the space between characters. So I think you need to do something like:

  1. get the contentsize.width() of the label
  2. divide this by the number of characters in your string
  3. This gives you how much space each character should take up or have padding added to each side so that it does take up that space.

okay we will try and let you know, mean while if you got any sample or example anyone have ever done then please let me know :slight_smile:

I can try to play with this if I have time, but I’m pretty busy these days. Perhaps start with cocos new and just add a label and start playing around. If you get close, send me the code that I can just drop in to a new project and help too.

@slackmoehrle oh that’s great, well we were trying and find this code from stack overflow

and it’s work with CPP and when we pass the string to this function and print with cocos2d::log() it print with justify, but then when we pass it to label, richtext it just doesn’t work, can you look into it we are so close, hope you will get time…

some screenshot that we did experiment

this is in the terminal with log() function and belove is with the label in cocos2dx

Wow glad the concept at least works.

Can you share your code with me so I can play with it?

@slackmoehrle thanks again for reply, this function we are using , and return paragraph with justify

typedef std::list<std::string> WordList;
const int pageWidth = 40;
std::string mainString;
std::vector<std::string> strings;

std::string Singleton::justifyText( const std::string &text )
    WordList words = splitTextIntoWords(text);

    std::string line;
    for (const std::string& word : words) {
        if (line.size() + word.size() + 1 > pageWidth) { // next word doesn't fit into the line.
            line = word;
        } else {
            if (!line.empty()) {
                line.append(" ");

    mainString = mainString + line;

  cocos2d::log("output here: %s", mainString.c_str());

  return mainString;

void Singleton::justifyLine( std::string line )
    size_t pos = line.find_first_of(' ');
    if (pos != std::string::npos) {
        while (line.size() < pageWidth) {
            pos = line.find_first_not_of(' ', pos);
            line.insert(pos, " ");
            pos = line.find_first_of(' ', pos+1);
            if (pos == std::string::npos) {
                pos = line.find_first_of(' ');
//    std::cout << line << std::endl;
    mainString = mainString + line + '\n';
    cocos2d::log("output : %s", line.c_str());


WordList Singleton::splitTextIntoWords( const std::string &text )
    WordList words;
    std::istringstream in(text);
    return words;

and we are calling this function like this

node->ComplainWidget = Widget::create();

auto verString = Singleton::getInstance()->justifyText(Language::getInstance()->getStringForKey1("PokerComplainPara1"));

//Poker Complain Para 1
Label *ComplainPara1 = Label::createWithSystemFont(verString , "Helvetica.ttf", frameSize.height * textSize);
ComplainPara1->setDimensions((ComplainPara1->getContentSize().width) , 0);
ComplainPara1->setLineSpacing(frameSize.height * 0.006f); //Spacing between two lines in para

so when we print

cocos2d::log(“output here: %s”, mainString.c_str());


std::string Singleton::justifyText( const std::string &text ){…}

it prints perfectly but when we are setting it to label right side not justifying somehow

if you need any help just ping us :slight_smile:

Do you have a cpp and header I can drop into a project?

yes wait, I am sending it

PM is fine.

this is the singleton file… and you can call it from thereSingleton.cpp (12.3 KB) Singleton.h (3.2 KB)

you can call

auto verString = Singleton::getInstance()->justifyText(“Poker is a family of card games that combines gambling, strategy and skill.All poker Variant involve betting as an intrinsic part of play and determine the winner of each hand according to the combinations of players’ cards, at least some of which remain hidden untill the end of the hand.Poker games vary in the number of cards dealt,the number of shared or ‘community’ cards, the number of cards that remain hidden,and the betting procedures.”);

1 Like

Thanks. I will find some time to play with this so we can make a feature request on GitHub issues.

1 Like