首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iOS 6和iOS 7中的CCLabelTTF contentSize不同- cocos2d-x

iOS 6和iOS 7中的CCLabelTTF contentSize不同- cocos2d-x
EN

Stack Overflow用户
提问于 2014-03-20 23:40:21
回答 3查看 611关注 0票数 1

我在我的项目中使用了Cocos2dx。我注意到CCLabelTTF绘制的文本比iOS 7高2-3个像素。iOS 6中的行空间也比iOS 7中的行空间大。我在两个不同的设备上进行了测试。代码很简单:

代码语言:javascript
运行
复制
CCLabelTTF *fLabel = CCLabelTTF::create(title, "Helvetica Bold", 14);
fLabel->setPosition(centerPoint);
node->addChild(fLabel);

有人知道怎么解决这个问题吗?

EN

回答 3

Stack Overflow用户

发布于 2014-03-24 23:58:38

回答我自己的问题。我找到了解决这个问题的办法。我现在使用的是cocos2dx 2.2,CCImage.mm中有一个bug。Cocos2dx使用已弃用的方法获取字符串大小。这就是为什么iOS 6中的字符串大小与iOS 7中的不同。我在CCImage.mm文件中编辑了_calculateStringSize方法,以下是我的代码:

代码语言:javascript
运行
复制
static CGSize _calculateStringSize(NSString *str, id font, CGSize *constrainSize)
{
    NSArray *listItems = [str componentsSeparatedByString: @"\n"];
    CGSize dim = CGSizeZero;
    CGSize textRect = CGSizeZero;
    textRect.width = constrainSize->width > 0 ? constrainSize->width
                                          : 0x7fffffff;
    textRect.height = constrainSize->height > 0 ? constrainSize->height
                                          : 0x7fffffff;

      CGSize tmp;
    if([str respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]){
        NSDictionary *attributes = @{
                NSFontAttributeName: font
        };
        tmp = [str boundingRectWithSize:textRect options:(NSStringDrawingOptions)(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:attributes context:nil].size;
        [paragraphStyle release];
    }else{
        tmp = [str sizeWithFont:font constrainedToSize:textRect];
    }

    if (tmp.width > dim.width)
    {
       dim.width = tmp.width; 
    }

    dim.height += tmp.height;


   return dim;
}

我建议您在项目中使用此方法来计算字符串大小。希望它能对某些人有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2014-03-22 14:51:43

代码语言:javascript
运行
复制
multiply the scale value to font size.

try this 

CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
float frameSize = pEGLView->getFrameSize();
float scaleFactor = frameSize.width / designResolutionSize.width ;
CCLabelTTF *fLabel = CCLabelTTF::create(title, "Helvetica Bold", 14*scaleFactor);
票数 0
EN

Stack Overflow用户

发布于 2014-06-23 19:03:12

Timur Mustafaev的实现有一些小问题。下面的代码应该可以正常工作:

代码语言:javascript
运行
复制
static CGSize _calculateStringSize(NSString *str, id font, CGSize *constrainSize)
{
    NSArray *listItems = [str componentsSeparatedByString: @"\n"];
    CGSize dim = CGSizeZero;
    CGSize textRect = CGSizeZero;
    textRect.width = constrainSize->width > 0 ? constrainSize->width
                                              : 0x7fffffff;
    textRect.height = constrainSize->height > 0 ? constrainSize->height
                                              : 0x7fffffff;


    for (NSString *s in listItems)
    {
        CGSize tmp;

        // Method only exists on iOS6+.
        if([s respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]){
            NSDictionary *attributes = @{NSFontAttributeName: font};
            tmp = [s boundingRectWithSize:textRect options:(NSStringDrawingOptions)(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:attributes context:nil].size;
        } else {
            tmp = [s sizeWithFont:font constrainedToSize:textRect];
        }

        if (tmp.width > dim.width)
        {
           dim.width = tmp.width; 
        }

        dim.height += tmp.height;
    }

    dim.width = ceilf(dim.width);
    dim.height = ceilf(dim.height);

    return dim;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22537998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档