Hi,
I’m pretty new to cocos2d-x. Just started today. My project have a need to display texts mixed of fonts and colors in multiple line. I find RichText might fit my needs (and that’s one of the reason I made a switch to Cocos2d-x).
I tried to create RichText using the follwing codes:-
auto text = cocos2d::ui::RichText::create();
const auto font = std::string("fonts/CSPrakas.otf");
text->pushBackElement(ui::RichElementText::create(0, Color3B::BLUE, 0xFF, "When I was young, ", font, 32));
text->pushBackElement(ui::RichElementText::create(0, Color3B::GREEN, 0xFF, "I listened to the radio.\nWaiting for my favorite song ", font, 32));
I expected to have something like (color omitted)
When I was young, I listened to the radio
Waiting for my favorite song
Instead I’ve got
Looking through the code, it looks like each RichTextElement has its own Label object (as a renderer). RichText is kinda like a collection of Label objects. So when one of them contains line break, the line is broken within the Label object instead of the whole RichText object.
Anyway, is RichText supposed to support multiple lines of text ? Or should I just use multiple RichText objects instead ? I’d just want to ask if it’s design decision or not. If it’s really a bug I’ll report in so someone (or even myself) might fix it. Or maybe there is something else I should use instead of RichText class.
If multi-line is supposed to be supported, I think the proper fix would be to break RichTextElementText further into multiple Label object at the line break character (like \n), then position it accordingly. But then it needs to be determine where the base line of the next line would be (in the case when multiple fonts/font sizes are in used in the RicthText object). I don’t see the baseline value get exposed from Label class anyway.
This class seems to be complicated to implement properly ![]()
PS. I’ve also found that when using multiple fonts in RichText, the baseline is not aligned. That might because currently there’s no way to know where the baseline of each Label objects are. (sorry for non-English text in the screenshot. I’m testing multiple languages text right now).





.