CCLabelTTF with zero height dimension incorrectly reports contentsize

If I set the height dimension parameter of a CCLabelTTF to make it wrap text within a width the contentSize is incorrectly reported making it difficult to position another label below the first. Example code follows:

@ const char * DescriptionText =“Line1\nLine2\nLine3\nLine4”;
CCLabelTTF descriptionLabel = CCLabelTTF::labelWithString,0), CCTextAlignmentLeft, “VAGRoundedStd-Light.otf”, 14.0);
descriptionLabel~~>setPosition.height));
descriptionLabel~~>setAnchorPoint);
descriptionLabel~~>setColor);
textLayer~~>addChild;
CCLog.height);
//Cocos2d: descriptionLabel height:60.000000
//Displays 4 lines on screen… each line is 15 high.
const char
DescriptionText =“Line1 should wrap round abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg\nLine2 should wrap round abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg 3\nLine4”;

//Cocos2d: descriptionLabel height:420.000000
//Displays 10 lines on screen… each line is 42 high
@

In the first example where there is no wrapping of text the contentSize is reported correctly.

In the second example where wrapping does occur the contentSize is much to large.

Am I doing something wrong or is this a bug.

Using cocos2d-1.0.1-x-0.9.2 results are the same on iOS and Android.

Any help would be appreciated!

I tested your code on win32 by using the same cocos2d-x version(cocos2d-1.0.1-x-0.9.2), I get the correct result.

first example :

descriptionLabel height:68.000000
//Displays 4 lines on screen... each line is 17 high 

sencond example :

descriptionLabel height:170.000000
//Displays 10 lines on screen... each line is 17 high 

Hi James

Thanks for running the test on Win32. That confirms that it should work as I assumed and I’m not doing something stupid ! It also narrows down where to look for the problem.

I’ll have a dig around comparing what’s different between the Win32 build and the iOS/Android builds.

Matt

I have also tested it on android, it works fine.

01-06 17:30:06.535: D/cocos2d-x debug info(6575): descriptionLabel height:140.000000

Ahh, okay that’s weird. Maybe it’s something else I’m doing.

I’ll start from scratch with a new project.

Thanks for the feedback.

Did you modify the cocos2d-x source?

Hi James

No, I haven’t modified any of the cocos2d-x source (at least none that would effect this).

I have tracked down the issue though (and am feeling a bit dumb for not thinking of this first!).

Thanks so much for your input.

I created a new project and pasted the code from the problem project. Running the new project gave the correct result. Gradually adding more and more bits from the problem project (sprite sheets, custom classes etc etc) until eventually it started giving the wrong result again.

Turns out the problem is with the VAGRoundedStd-Light.otf font I am using. If I switch back to a “normal” font like Helvetica everything works as it should.

I should have eliminated the font first, it’s an obvious potential problem !

When I get time I’ll try some different fonts (otf / ttf) to see if it’s this particular font or any added font.

All the best and thanks again.

Matt

I just quickly tried a couple different otf and ttf fonts and the results are as follows.

OCRAStd.otf - reports incorrect height
Chalkduster.ttf - reports incorrect height
NuevaStd-Cond.otf - reports incorrect height
VAGRoundedStd-Light.otf - reports incorrect height
Courier New.ttf - reports incorrect height

Built in fonts
Helvetica - works fine
Marker Felt - works fine

I’ll stick to using built in fonts for now…

Any work around for this while using custom fonts and not the built-in fonts? I have to use a custom ttf in my game.

Any work around for this while using custom fonts and not the built-in fonts? I have to use a custom ttf in my game.