Stroking not working on CCLabelTTF in cocos2d-x ios in c++

二次信任 提交于 2020-01-03 05:20:35

问题


I am tryng to make an outline of green color in my label but its not working.. my code is

CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "HoboStd", 50);
pLabel->setPosition(ccp(200,200));
pLabel->enableStroke(ccGREEN, 5.0,true);
this->addChild(pLabel);

Its not providing the outline around the label text Hello World.Any one here who can help me


回答1:


I got the fix in ios7.0 on how to enable stroking on labels 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

Step1

Find the CCImage.mm file in your project and find the following code written there

//actually draw the text in the context
//XXX: ios7 casting
[str drawInRect:CGRectMake(textOriginX, textOrigingY, textWidth, textHeight) withFont:font   
lineBreakMode:NSLineBreakByWordWrapping alignment:(NSTextAlignment)align];

Step2

Now add the following code just below this line

//New 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];
}
//New Code End

Save the CCImage.mm file and then again re-run your project. It will redraw stroke with correct color.Tested on 7.0 simulator




回答2:


I tested your code and I get the outline. Try to reduce the stroke width to 1.0.

pLabel->enableStroke(ccGREEN, 1.0,true);


来源:https://stackoverflow.com/questions/21720431/stroking-not-working-on-cclabelttf-in-cocos2d-x-ios-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!