2

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

4

2 回答 2

1

不幸的是,就像Ant说的那样,这是一个 iOS 特有的功能,你想避免在它前面使用任何带有 UI 或 NS 的东西。

话虽如此,有一种方法可以用字体和大小计算文本的边界

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

你可以创建一个这样的函数

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;
}
于 2014-07-03T07:58:26.830 回答
0

你指的方法是[UIFont fontWithSize],它是一个UIKit 方法,与Cocos2d 无关。

由于这是 iOS 的一个特性,而不是 Cocos2d(如果你使用 UIKit,可能是你的菜单的其余部分),你会发现你需要使用 Cocos2d-x 本身来构建你的菜单,而不是。

这里有一个使用标签的教程:

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

于 2013-06-03T19:34:43.190 回答