android - 适用于 Android 的 sizeWithFont

标签 android cocos2d-x cocos2d-android sizewithfont

我正在将一个 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/

相关文章:

c++ - 复制 CCSprite

android - 如何在Cocos2D-Android中创建无图像的可见矩形?

android - 生成 token 以访问谷歌帐户

c++ - rapidjson::std::pair 中的文档

android - Cocos2dx : push notification when app is clossing

android - 在 cocos2d android 中打开应用程序对话框时自动关闭应用程序

android - 无法在 Cocos2d for android 上设置 PixelFormat

android - 如何建立从 iOS Safari 到 Android Chrome 的 Webrtc 连接

android - 将 TextView 数组动态添加到线性布局

javascript - 在 android Webview 中添加 ReactJS