Empty line in a multiline label


#1

Hi all,

I found that a multiline CCLabelTTF will drop its last line if there is an empty line in the text (“”).
Here is an example (see the attached file) :

addChild(CCLabelTTF::create(“2 1 LAST”, FONT_NAME, FONT_SIZE), 1, 1);
getChildByTag(1)->setPosition(CCPoint(500, 500));
addChild(CCLabelTTF::create(“2 1 LAST”, FONT_NAME, FONT_SIZE), 1, 2);
getChildByTag(2)->setPosition(CCPoint(700, 500));

The temporary solution I found is to add a space in the line (“”).

Thanks in advance.


IMG_0032.PNG (29.0 KB)


#2

Hi, could you tell me which platform and engine version you are using?


#3

Of course,

We’re using cocos2d-2.0-x-2.0.3, the screenshot is made on an iPad3 running iOS 5.1.1.


#4

Hi,

I believe I know the cause of this issue, and have a possible fix.
The problem code is the function calculateStringSize in CCImage.mm.
The current code splits the line into separate lines and calculates the height of each line. The individual line heights are then added. The problem is that any empty lines report a height of 0.
Calculating the text height in a single block seems to fix the issue. The following function shows the new code.
<pre>
static CGSize
calculateStringSize(NSString str, id font, CGSizeconstrainSize)
{
CGSize dim = CGSizeZero;
CGSize textRect = CGSizeZero;
textRect.width = constrainSize~~>width > 0 ? constrainSize~~>width
: 0x7fffffff;
textRect.height = constrainSize~~>height > 0 ? constrainSize~~>height
: 0x7fffffff;

dim = [str sizeWithFont:font constrainedToSize:textRect];

return dim;
}

I haven’t found any side effects of this changes yet. Please also note that any trailing empty lines will not be included in the calculated height of the text.

Hope this helps.
Dion


#5

Well, it works.
Thank you !


#6

this is still broken in 2.1.2