Friday 14 February 2014

How to enable stroking on label in IOS 7.0 in cocos2d-x

The normal stroking code of a label doesn’t work in IOS 7.0 but it works successfuly below IOS 7.0
The basic code for enabling stroking is provided below. We need to add a CCLabelTTF and then call the enableStroke function

CCLabelTTF *label=CCLabelTTF::create("Hello", "Arial.fnt", 50);
label->setPosition(ccp(300,300));
label->enableStroke(ccGREEN, 1.0,true);
this->addChild(label);

This works fine with IOS below 7.0. But in IOS 7.0 there is no stroking effect on the label. In order to fix the issue just follow some steps
1. Find the CCImage.mm file in your project and find the following code written there
[str drawInRect:CGRectMake(textOriginX, textOrigingY, textWidth, textHeight) withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:(NSTextAlignment)align];
2. Now add the following code just below this line
// Code Start
if(pInfo->hasStroke)
{
CGContextSetTextDrawingMode(context, kCGTextStroke);
CGContextSetRGBFillColor(context, pInfo->strokeColorR, pInfo->strokeColorG, pInfo->strokeColorB, 1);
CGContextSetLineWidth(context, pInfo->strokeSize);
[str drawInRect:CGRectMake(textOriginX, textOrigingY, textWidth, textHeight) withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:(NSTextAlignment)align];
}
//Code End
3. Save the CCImage.mm file and then again re-run your project. It will redraw stroke with correct color.
Tested on 7.0 simulator
HAPPY CODING