【问题标题】:sizeWithFont for Android适用于 Android 的 sizeWithFont
【发布时间】:2013-06-03 19:27:42
【问题描述】:

我正在将一个cocos2d iOS 游戏移植到cocos2d-x Android 并且cocos2d 游戏代码有这个方法sizeWithFont,但是我没有这个方法可以在Android 中使用。那么在使用Cocos2d-x for Android的时候可以用什么方法来替换sizeWithFont

【问题讨论】:

    标签: android cocos2d-x cocos2d-android sizewithfont


    【解决方案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;
    }
    

    【讨论】:

      【解决方案2】:

      你指的方法是[UIFont fontWithSize],是一个UIKit方法,和Cocos2d没有关系。

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

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

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

      【讨论】:

        猜你喜欢
        • 2012-01-27
        • 2013-01-19
        • 1970-01-01
        • 1970-01-01
        • 2018-07-14
        • 2018-07-21
        • 2011-09-17
        • 2012-01-14
        • 1970-01-01
        相关资源
        最近更新 更多