Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For those with Cocos 2d 2.x, I changed @Hardschool code to fix the deprecated methods and it's working awesome!</p> <p>in the .h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import "cocos2d.h" @interface CCLabelTTFLineSpaced : CCLayer { } + (id) labelWithString:(NSString*)string dimensions:(CGSize)dimensions hAlignment: (CCTextAlignment)alignment fontName:(NSString*)name fontSize:(CGFloat)size lineSpace:(CGFloat)space; - (id) initWithString:(NSString*)str dimensions:(CGSize)dimensions hAlignment:(CCTextAlignment)alignment fontName:(NSString*)name fontSize:(CGFloat)size lineSpace:(CGFloat)space; @end </code></pre> <p>in the .m file</p> <pre><code>#import "CCLabelTTFLineSpaced.h" @implementation CCLabelTTFLineSpaced + (id) labelWithString:(NSString*)string dimensions:(CGSize)dimensions hAlignment:(CCTextAlignment)alignment fontName:(NSString*)name fontSize:(CGFloat)size lineSpace:(CGFloat)space{ return [[[self alloc] initWithString: string dimensions:dimensions hAlignment:alignment fontName:name fontSize:size lineSpace:(CGFloat)space]autorelease]; } - (id) initWithString:(NSString*)str dimensions:(CGSize)dimensions hAlignment:(CCTextAlignment)alignment fontName:(NSString*)name fontSize:(CGFloat)size lineSpace:(CGFloat)space{ if( (self=[super init]) ) { anchorPoint_ = ccp(0.5f, 0.5f); [self setContentSize:dimensions]; self.ignoreAnchorPointForPosition = YES; int pos = 0; int i = 0; while (pos&lt;[str length]) { int end = 0; int lastCut = -1; bool finished=NO; while (finished==NO) { CGSize actualSize = [[str substringWithRange:NSMakeRange(pos, end)] sizeWithFont:[UIFont fontWithName:name size:size]]; if (actualSize.width &gt; dimensions.width || pos+end == [str length]) { if (pos+end == [str length] &amp;&amp; actualSize.width &lt;= dimensions.width) lastCut = end; finished=YES; } else { if ([[str substringWithRange:NSMakeRange(pos+end, 1)] isEqualToString:@" "] || [[str substringWithRange:NSMakeRange(pos+end, 1)] isEqualToString:@"."] || [[str substringWithRange:NSMakeRange(pos+end, 1)] isEqualToString:@","]) { lastCut = end; } end++; } } NSString * strLine = [str substringWithRange:NSMakeRange(pos, lastCut)]; CCLabelTTF * line = [CCLabelTTF labelWithString:strLine dimensions:CGSizeMake(dimensions.width, size*2) hAlignment:alignment fontName:name fontSize:size]; [line setAnchorPoint:ccp(0,1)]; [line setPosition:ccp(0,-i*space)]; [self addChild:line]; pos=pos+lastCut; i++; } } return self; } @end </code></pre> <p>That's it, thanks @Hardschool!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload