我正在将一个 cocos2d iOS 游戏移植到 cocos2d-x Android 并且 cocos2d 游戏代码有这个方法 sizeWithFont,但我没有这个方法可以在 Android 中使用。那么在使用Cocos2d-x for Android时可以用什么方法来替换sizeWithFont
最佳答案
不幸的是,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;
}
关于android - 适用于 Android 的 sizeWithFont,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16904364/