diff --git a/AUTHORS b/AUTHORS index f4b0b2cfb7..888814e7de 100644 --- a/AUTHORS +++ b/AUTHORS @@ -24,6 +24,7 @@ Developers: carlomorgantinizynga CCLabelTTF supports for shadow and stroke + Adding CCLabelTTF::createWithFontDefinition. James Gregory (j4m3z0r, Zynga) Maintainer of Emscripten port. diff --git a/cocos2dx/label_nodes/CCLabelTTF.cpp b/cocos2dx/label_nodes/CCLabelTTF.cpp index dd375118b1..436094bef6 100644 --- a/cocos2dx/label_nodes/CCLabelTTF.cpp +++ b/cocos2dx/label_nodes/CCLabelTTF.cpp @@ -96,6 +96,18 @@ CCLabelTTF* CCLabelTTF::create(const char *string, const char *fontName, float f return NULL; } +CCLabelTTF * CCLabelTTF::createWithFontDefinition(const char *string, ccFontDefinition &textDefinition) +{ + CCLabelTTF *pRet = new CCLabelTTF(); + if(pRet && pRet->initWithStringAndTextDefinition(string, textDefinition)) + { + pRet->autorelease(); + return pRet; + } + CC_SAFE_DELETE(pRet); + return NULL; +} + bool CCLabelTTF::init() { return this->initWithString("", "Helvetica", 12); diff --git a/cocos2dx/label_nodes/CCLabelTTF.h b/cocos2dx/label_nodes/CCLabelTTF.h index a5f950d5c3..487a89b0c2 100644 --- a/cocos2dx/label_nodes/CCLabelTTF.h +++ b/cocos2dx/label_nodes/CCLabelTTF.h @@ -70,6 +70,10 @@ public: const CCSize& dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment); + + /** Create a lable with string and a font definition*/ + static CCLabelTTF * createWithFontDefinition(const char *string, ccFontDefinition &textDefinition); + /** initializes the CCLabelTTF with a font name and font size */ bool initWithString(const char *string, const char *fontName, float fontSize); diff --git a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp index fc0ea34a4c..bdb99ddc02 100644 --- a/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp +++ b/samples/Cpp/TestCpp/Classes/LabelTest/LabelTest.cpp @@ -1470,7 +1470,7 @@ TTFFontShadowAndStroke::TTFFontShadowAndStroke() addChild(layer, -10); CCSize s = CCDirector::sharedDirector()->getWinSize(); - + ccColor3B tintColorRed = { 255, 0, 0 }; ccColor3B tintColorYellow = { 255, 255, 0 }; ccColor3B tintColorBlue = { 0, 0, 255 }; @@ -1479,9 +1479,6 @@ TTFFontShadowAndStroke::TTFFontShadowAndStroke() CCSize shadowOffset(12.0, 12.0); - // create the label shadow only - CCLabelTTF* fontShadow = new CCLabelTTF(); - ccFontDefinition shadowTextDef; shadowTextDef.m_fontSize = 20; shadowTextDef.m_fontName = std::string("Marker Felt"); @@ -1492,17 +1489,16 @@ TTFFontShadowAndStroke::TTFFontShadowAndStroke() shadowTextDef.m_shadow.m_shadowBlur = 1.0; shadowTextDef.m_fontFillColor = tintColorRed; - fontShadow->initWithStringAndTextDefinition("Shadow Only Red Text", shadowTextDef); + // shadow only label + CCLabelTTF* fontShadow = CCLabelTTF::createWithFontDefinition("Shadow Only Red Text", shadowTextDef); // add label to the scene this->addChild(fontShadow); fontShadow->setPosition(ccp(s.width/2,s.height/4*2.5)); - - // create the label stroke only - CCLabelTTF* fontStroke = new CCLabelTTF(); - + + // create the stroke only label ccFontDefinition strokeTextDef; strokeTextDef.m_fontSize = 20; strokeTextDef.m_fontName = std::string("Marker Felt"); @@ -1513,7 +1509,8 @@ TTFFontShadowAndStroke::TTFFontShadowAndStroke() strokeTextDef.m_fontFillColor = tintColorYellow; - fontStroke->initWithStringAndTextDefinition("Stroke Only Yellow Text", strokeTextDef); + // stroke only label + CCLabelTTF* fontStroke = CCLabelTTF::createWithFontDefinition("Stroke Only Yellow Text", strokeTextDef); // add label to the scene this->addChild(fontStroke); @@ -1522,8 +1519,6 @@ TTFFontShadowAndStroke::TTFFontShadowAndStroke() // create the label stroke and shadow - CCLabelTTF* fontStrokeAndShadow = new CCLabelTTF(); - ccFontDefinition strokeShaodwTextDef; strokeShaodwTextDef.m_fontSize = 20; strokeShaodwTextDef.m_fontName = std::string("Marker Felt"); @@ -1539,13 +1534,16 @@ TTFFontShadowAndStroke::TTFFontShadowAndStroke() strokeShaodwTextDef.m_fontFillColor = tintColorBlue; - - fontStrokeAndShadow->initWithStringAndTextDefinition("Stroke & Shadow Blue Text", strokeShaodwTextDef); + + // shadow + stroke label + CCLabelTTF* fontStrokeAndShadow = CCLabelTTF::createWithFontDefinition("Stroke & Shadow Blue Text", strokeShaodwTextDef); // add label to the scene this->addChild(fontStrokeAndShadow); fontStrokeAndShadow->setPosition(ccp(s.width/2,s.height/4*1.1)); + + } std::string TTFFontShadowAndStroke::title()