Reputation: 4929
I'm using Cocos2dx
in my project. And I've noticed that CCLabelTTF
draws text in 2-3 pixels higher than in iOS 7
. Line space in iOS 6
is also bigger than in iOS 7
.
I was tested that on 2 different devices. Code is simple:
CCLabelTTF *fLabel = CCLabelTTF::create(title, "Helvetica Bold", 14);
fLabel->setPosition(centerPoint);
node->addChild(fLabel);
Does anyone knows how to fix this?
Upvotes: 1
Views: 623
Reputation: 636
There were minor issues with Timur Mustafaev's implementation. 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;
}
Upvotes: 0
Reputation: 4929
Answering my own question. I found solution of that. I'm using cocos2dx 2.2 now and there is bug in CCImage.mm
.
Cocos2dx using deprecated method to get string size. 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:
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.
Upvotes: 1
Reputation: 469
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);
Upvotes: 0