Question

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

Was it helpful?

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;
}

OTHER TIPS

The method you are referring to is [UIFont fontWithSize], which is a UIKit method and has nothing to do with Cocos2d.

Since this is a feature of iOS and not Cocos2d (as is, presumably, the rest of your menu if you are using UIKit), you will find that you will need to build your menus using Cocos2d-x itself, instead.

There is a tutorial on working with labels here:

http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Text_Labels#Creating-labels-Simple-way

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top