[BUG] CCLabelTTF cut text when it contains "\n\n" on IOS

[BUG] CCLabelTTF cut text when it contains "\n\n" on IOS
0.0 0


Hi all!

I think that I found a bug in CCLabelTTF (cocos2d-x 2.2)

I want create label with blank line in the middle using string like “some text\n\nsometext”. On Win32 all works properly but on iOS (any device) last line disappeared.

This sample code reproduce problem:

string text = "Lorem ipsum dolor sit amet, consectetuer \n\nadipiscing elit. Aenean commodo ligula eget dolor. \n\nAenean massa. Cum sociis natoque \n\npenatibus et magnis dis parturient montes, nascetur ridiculus mus\n\nend of text";
CCLabelTTF * label = CCLabelTTF::create(text.c_str(), "Arial", 14, CCSize(100,0), kCCTextAlignmentLeft, kCCVerticalTextAlignmentTop);

We lost one line for each “”.

I take a look into calculateStringSize method of CCImage.mm and found:
static CGSize
calculateStringSize(NSString str, id font, CGSizeconstrainSize)
NSArray *listItems = [str componentsSeparatedByString: @“”];
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 = [s sizeWithFont:font constrainedToSize:textRect]; // <— Warning
// this method deprecated in iOS 7

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

dim.height = tmp.height; // <— HERE we have 0 for blank line
// but must have + one line height

return dim;

for (NSString *s in listItems)

CGSize tmp2 = [str sizeWithFont:font constrainedToSize:textRect];
dim.height = tmp2.height;

I added…



I reply because I had a similar problem and found the solution : sometime, the last line get cut. This is caused by some float~~>int conversion later on that loose some information and may cut an entire line.
A simple solution is to replace
<pre>dim.height = tmp.height;</pre>
<pre>dim.height= ceil;</pre>
This will round up the height to avoid any problem with float~~>int conversion.