我在我的项目中使用了Cocos2dx
。我注意到CCLabelTTF
绘制的文本比iOS 7
高2-3个像素。iOS 6
中的行空间也比iOS 7
中的行空间大。我在两个不同的设备上进行了测试。代码很简单:
CCLabelTTF *fLabel = CCLabelTTF::create(title, "Helvetica Bold", 14);
fLabel->setPosition(centerPoint);
node->addChild(fLabel);
有人知道怎么解决这个问题吗?
发布于 2014-03-24 23:58:38
回答我自己的问题。我找到了解决这个问题的办法。我现在使用的是cocos2dx 2.2,CCImage.mm
中有一个bug。Cocos2dx使用已弃用的方法获取字符串大小。这就是为什么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;
}
我建议您在项目中使用此方法来计算字符串大小。希望它能对某些人有所帮助。
发布于 2014-03-22 14:51:43
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);
发布于 2014-06-23 19:03:12
Timur Mustafaev的实现有一些小问题。下面的代码应该可以正常工作:
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;
}
https://stackoverflow.com/questions/22537998
复制相似问题