[英]CCLabelTTF contentSize different in iOS 6 and iOS 7 - cocos2d-x
I'm using Cocos2dx
in my project. 我在我的项目中使用
Cocos2dx
。 And I've noticed that CCLabelTTF
draws text in 2-3 pixels higher than in iOS 7
. 而且我注意到
CCLabelTTF
绘制的文本比iOS 7
高2-3像素。 Line space in iOS 6
is also bigger than in iOS 7
. iOS 6
行空间也大于iOS 7
行空间。 I was tested that on 2 different devices. 我在2种不同的设备上进行了测试。 Code is simple:
代码很简单:
CCLabelTTF *fLabel = CCLabelTTF::create(title, "Helvetica Bold", 14);
fLabel->setPosition(centerPoint);
node->addChild(fLabel);
Does anyone knows how to fix this? 有谁知道如何解决这个问题?
Answering my own question. 回答我自己的问题。 I found solution of that.
我找到了解决方案。 I'm using cocos2dx 2.2 now and there is bug in
CCImage.mm
. 现在,我使用cocos2dx 2.2和有缺陷的
CCImage.mm
。 Cocos2dx using deprecated method to get string size. Cocos2dx使用不推荐使用的方法来获取字符串大小。 That's why string size in iOS 6 is different than in iOS 7. I've edited
_calculateStringSize
method in CCImage.mm
file, here is my code: 这就是为什么iOS 6中的字符串大小与iOS 7中的字符串大小不同的原因。我在
CCImage.mm
文件中编辑了_calculateStringSize
方法,这是我的代码:
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;
}
And I suggest you to use this method to calculate string size in your projects. 我建议您使用此方法来计算项目中的字符串大小。 Hope it will helps for someone.
希望它对某人有帮助。
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);
There were minor issues with Timur Mustafaev's implementation. Timur Mustafaev的实施存在一些小问题。 This one should work correctly:
这应该可以正常工作:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.