Set CCLabelTTF bold?

Is there any way in cocos2d-x (i’m using 2.1.4 version) to set the font bold? I have texts as CCLabelTTF objects and I need to set them bold for some time, as highlighting.

You can give texts a bold effect by creating stroke of same color as label.

this->addChild( createStroke(lbl, 0.4, lbl->getColor(), 255) );

For more details about stroke, refer this link

If you use cocos2d-x 3.x :slight_smile:

you can use cocos2d::ui::RichText::create() , or cocos2d::label::createWithTTF(…)

Method 1: (use RichText)
example :

auto text = cocos2d::ui::RichText::create();
const auto font = std::string(“fonts/CSPrakas.otf”);
int flag = 2 ;
text->pushBackElement(ui::RichElementText::create(0, Color3B::BLUE, 0xFF, "When I was young, ", font, 32, flag));

flag =1 -> ITALICS_FLAG
flag =2 -> BOLD_FLAG
flag =4 ->UNDERLINE_FLAG
flag =8 ->STRIKETHROUGH_FLAG

you can see it , in UIRichText.h

enum {
ITALICS_FLAG = 1 << 0, /*!< italic text /
BOLD_FLAG = 1 << 1, /
!< bold text /
UNDERLINE_FLAG = 1 << 2, /
!< underline /
STRIKETHROUGH_FLAG = 1 << 3, /
!< strikethrough /
URL_FLAG = 1 << 4, /
!< url of anchor /
OUTLINE_FLAG = 1 << 5, /
!< outline effect /
SHADOW_FLAG = 1 << 6, /
!< shadow effect /
GLOW_FLAG = 1 << 7 /
!< glow effect */
};

Method 2 : ( use label::createWithTTF)

cocos2d::TTFConfig config;
config.fontSize = 55;
config.fontFilePath = “fonts/UVNThoiDai2.TTF”;
config.bold = true;
config.italics = true;
auto lab = Label::createWithTTF(config, “HAHAHAHA hehehe”, cocos2d::TextHAlignment::CENTER);
lab->setPosition(Vec2(500,500));
addChild(lab);

Method 3:

lab ->enableBold();