Wrapping Text in a Cocos2d CCLabel


Wrapping Text in a Cocos2d CCLabel

by Christopher Kaukis

Have you ever wanted to insert a new line character or set the appropriate height for some font size when you know the width to wrap text on a CCLabel in Cocos2d?

Here is a quick snippet on how to wrap your text in a CCLabel in both cases:

NSString *text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas non mi ante. Etiam dignissim sodales nibh, et posuere leo rutrum sed. Suspendisse nisl lectus, convallis at eleifend eget, molestie eu nunc.";

// self.contentSize.width is limiting the width of the label to this CCNode,
// in my case it's a new layer of a specific width and height and i don't
// want the text to go outside these demensions.
CGSize textSize = [text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:18.0f]
constrainedToSize:CGSizeMake(self.contentSize.width, CGFLOAT_MAX)

CCLabel *textLabel = [CCLabel labelWithString:text

More About the Author

Christopher Kaukis

Software Engineer
Running a Jekyll Blog Using Github Pages on Mac OS X 10.8 Mountain Lion In my last post I described installing Ruby on Mac OS X with RVM. The main reason I wanted to do this was to run a Jekyll Blog using ...
Installing Ruby 2.0.0 with RVM and Homebrew on Mac OS X 10.8 Mountain Lion This is a tutorial on how to install Ruby 2.0.0 on Mac OS X 10.8 Mountain Lion.  1. Install Xcode and the Command Line Tools If you ...

See more from this author →

InterWorks uses cookies to allow us to better understand how the site is used. By continuing to use this site, you consent to this policy. Review Policy OK


Interworks GmbH
Ratinger Straße 9
40213 Düsseldorf
Geschäftsführer: Mel Stephenson

Kontaktaufnahme: markus@interworks.eu
Telefon: +49 (0)211 5408 5301

Amtsgericht Düsseldorf HRB 79752
UstldNr: DE 313 353 072


Love our blog? You should see our emails. Sign up for our newsletter!