Search code examples
c++cocos2d-xcocos2d-x-3.0

How to create Label with Chinese and Japanese characters with custom color


I use cocos2d-x (c++) ver. 3.8 on tvOS (Apple TV)

I don't want to use TTF versions because I don't have decent Arial TTF font which includes Chinese and Japanese characters and yet has acceptable file size (up to 1MB).

If I use the Label::createWithSystemFont() this doesn't allow to change the text color. At least I did not find it how to change it.

auto *n =  Label::createWithSystemFont("洗牌位置", font, fontSize, dimensions, hAlignment, vAlignment);
n->setColor(Color3B::RED);
n->setTextColor(Color4B::RED);

This produces black text...

EDIT:

In a desperate situation I've tried something you might think I'm mad. But it doesn't work neither. The result is still black. Would you know ANY way how to change the color of the Label??

Node* NodeFactory::colorizeLabel (Label *lbl, const Color3B& color)
{
     auto *maskSprite = lbl;

    CCRenderTexture * rt;
        rt = CCRenderTexture::create(
                                     maskSprite->getContentSize().width*maskSprite->getScaleX(),
                                     maskSprite->getContentSize().height*maskSprite->getScaleY()
                                     , kCCTexture2DPixelFormat_RGBA8888
        );
        maskSprite->setPosition(ccp(
                                    (maskSprite->getContentSize().width*maskSprite->getScaleX())/2,
                                    (maskSprite->getContentSize().height*maskSprite->getScaleY())/2
                                    )
                                );


    //rt->begin();
    rt->beginWithClear(120, 120, 0, 0);
    ((Node*)maskSprite)->visit();
    rt->end();

    Sprite *retval = Sprite::createWithTexture(rt->getSprite()->getTexture());
    retval->setColor(Color3B::WHITE);
    //retval->setBlendFunc((ccBlendFunc) { GL_SRC_COLOR, GL_ONE });

    return retval;
}

Solution

  • FOUND solution for colored system font on tvOS: FIX: CCDevice-tvos.mm:

    ORIG:

    [str drawInRect:rect withAttributes:@{ NSFontAttributeName: font, NSParagraphStyleAttributeName:paragraphStyle }];
    

    NEW:

    [str drawInRect:rect withAttributes:@{ NSFontAttributeName: font, NSParagraphStyleAttributeName:paragraphStyle,
        NSForegroundColorAttributeName:[UIColor colorWithRed:info->tintColorR green:info->tintColorG blue:info->tintColorB alpha:info->tintColorA]
    }];