androidcocos2d-xcocos2d-androidsizewithfont

sizeWithFont for Android


I am porting a cocos2d iOS game to cocos2d-x Android and the cocos2d game code has this method sizeWithFont, but I do not have this method to use in Android. So what method can I use to replace sizeWithFont when using Cocos2d-x for Android


Solution

  • Unfortunately like Ant said that is a iOS specific feature, and you want to avoid using anything with UI or NS in front of it.

    That being said there is a way to calculate the bounds of text with a font and a size

    CCLabelTTF *label = CCLabelTTF::create("my text", "Arial", 48);
    CCLog("label size: %f,%f", timeLabel->boundingBox().size.width, timeLabel->boundingBox().size.height);
    

    You could create a function like this

    CCSize sizeWithFont(const char *string, const char *fontName, float fontSize) {
        CCLabelTTF *label = CCLabelTTF::create(string, fontName, fontSize);
        CCSize size = label->boundingBox().size;
        delete label;
        return size;
    }