CCLabelTTF - outside stroke on iOS and Android?

CCLabelTTF - outside stroke on iOS and Android?
0.0 0


I’m wondering if it’s possible to get an outside stroke using the current CCLabelTTF in cocos2d-x 2.1.5. Before the 2.1.x branch, we were using 2.0.4 and had to use a custom class to have an outside stroke, but I wanted to utilize the built-in stroke mechanism that 2.1.4 had.

Currently you can only get an inside stroke with it. On top of the stroke being on the inside, the size of the stroke is not the same size between iOS and Android. When I use a stroke of 3.0f, it’s a lot thicker on iOS than it is on Android.

I’m aware that the stroking mechanism only works on iOS and Android currently. But if there is some way, without a custom class, to get an outside stroke with these methods, I would like to be able to use it. If using a custom class is currently the only way, so be it, but avoiding it would be much more ideal.


I use this code, which was adapted from the original version at

CCRenderTexture* createTextStroke(CCLabelTTF *label, float size, ccColor3B cor)
    CCRenderTexture *rt = CCRenderTexture::create(label->getTexture()->getContentSize().width + size*2, label->getTexture()->getContentSize().height + size*2);

    CCPoint originalPos = label->getPosition();
    ccColor3B originalColor = label->getColor();
    bool originalVisibility = label->isVisible();

    ccBlendFunc originalBlend = label->getBlendFunc();
    label->setBlendFunc((ccBlendFunc) { GL_SRC_ALPHA, GL_ONE });

    CCPoint meio = ccp(label->getTexture()->getContentSize().width/2+size, label->getTexture()->getContentSize().height/2+size);
    for (int i=0; i<360; i+=20) // you should optimize that for your needs
        label->setPosition(ccp(meio.x + sin(CC_DEGREES_TO_RADIANS(i))*size, meio.y + cos(CC_DEGREES_TO_RADIANS(i))*size));


    return rt;

You can use it like this:

CCRenderTexture *textOutline = createTextStroke(gameTitle, 1.0f, ccc3(60, 0, 0));

You can use textOutline like it was any other CCNode, i.e. add it to your scene and position it behind the label you are stroking.

Hope that helps!



I probably should’ve mentioned in my original post that I was already using that code in my custom class. It does work nicely enough, I was just hoping to avoid needing to continue using it when there is a built-in method to do stroking, not realizing that the built-in method only did an inside stroke as opposed to an outside one like that code does.