1

I can use CCLabelTTF like this, (cocos2d-x v2.1.4)

CCLabelTTF* ableText = CCLabelTTF::create("hello red blue green", "Arial", 9);
ableText->setPosition(ccp(100, 100));
ableText->setAnchorPoint(ccp(0.5, 0.5));
ableText->setColor(ccc3(100, 100, 100));
ableText->setHorizontalAlignment(kCCTextAlignmentCenter);
this->addChild(ableText, 1);

But, I want to give different colors to the string.
"red" => red color
"blue" => blue color
"green" => green color

Is it possible using one CCLabelTTF?

Jinbom Heo
  • 7,248
  • 14
  • 52
  • 59

6 Answers6

4

You can change the color of each of the letters independently by accessing them as sprites like this

message_label->getLetter(6)->setColor(Color3B::RED);

IMPORTANT: I've only tried this with True Type Fonts

Pochi
  • 13,391
  • 3
  • 64
  • 104
3

stubma's 'CCRichLabelTTF' solution support this functionality using style tag.

 * TTF label which support different style in a string. By using a tag description such as:
 * 'Hell[color=ffff0000]o[/color]", the 'o' character will be in red color. If you want 
 * a '[' character, use '\' to escape.

check his github. Link

Jinbom Heo
  • 7,248
  • 14
  • 52
  • 59
1

No, you'll have to split up the string into individual labels per color.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
0
you can do that Using Simply CCLabelBMFont. Because you can simply Access the Each character using index value. 


CCLabelBMFont *lblInfo = [CCLabelBMFont labelWithString:@"Multi Color Label" fntFile:@"SSPro.fnt"];


then, you can Access the character using Index path.

  for (i=starting index; i<ending index; i ++) {

        CCSprite *charSprite = (CCSprite *)[[lblInfo children] objectAtIndex:i];
        charSprite.color = [CCColor redColor];
    }
set or change the color of character.
Renish Dadhaniya
  • 10,642
  • 2
  • 31
  • 56
  • Ok, so the user asked how to do this with a CCLabelTTF, but this answer is valid because CCLabelBMFont is the most common way represent a single string with multiple colors. This answer should not be voted down, but maybe Renish could have explained the differences between the two font choices a bit. – Kevin Ortman Jul 07 '14 at 01:20
  • @KevinOrtman,thank you very much for your suggestion.i am believe in how to do any things easily.So i suggest this way.Someone, suggest take different label for character separation.But, i am not doing this way because it's take more memory address and also it's handling is also difficult.I like comments so, we update my self and do better way things.thanks again... – Renish Dadhaniya Jul 07 '14 at 07:04
0

Don't Don't Don't set the color explicitly by using

 lablel->setTextColor();//don't do this

Instead set each letter color by using

lablel->getLetter(0)->setColor(cocos2d::Color3B(255, 0, 0));
jjpp
  • 1,298
  • 1
  • 15
  • 31
-1

You can also split up string by some symbol say "@Red" and then use the native iOS and Android methods to give the effect.

For iOS in CCImage.mm file in _initWithString , you can use NSMutableAttributedString and then split by those tags and then set the range color. With that you won't have to create different labels

For Android in Cocos2dXBitmap.java you can create new Paint object while iterating through the loop.

Let me know if that helps. Thanks

Dhaval
  • 301
  • 1
  • 3
  • 5