Line spacing in CCLabelTTF

Is there an application in iOS for setting the distance between several lines in CCLabelTTF in cocos2d? thank

+5
source share
4 answers

The answer to your question is no. You cannot configure the CCLabelTTF line of lines. But hey! I will share with you my decision for this;)

This is .h

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface CCLabelTTFLineSpaced : CCLayer {
}

+ (id) labelWithString:(NSString*)string dimensions:(CGSize)dimensions alignment:(CCTextAlignment)alignment fontName:(NSString*)name fontSize:(CGFloat)size lineSpace:(CGFloat)space;

- (id) initWithString:(NSString*)str dimensions:(CGSize)dimensions alignment:(CCTextAlignment)alignment fontName:(NSString*)name fontSize:(CGFloat)size lineSpace:(CGFloat)space;

@end

And this .m

#import "CCLabelTTFLineSpaced.h"

@implementation CCLabelTTFLineSpaced

+ (id) labelWithString:(NSString*)string dimensions:(CGSize)dimensions alignment:(CCTextAlignment)alignment fontName:(NSString*)name fontSize:(CGFloat)size lineSpace:(CGFloat)space{
    return [[[self alloc] initWithString: string dimensions:dimensions alignment:alignment fontName:name fontSize:size lineSpace:(CGFloat)space]autorelease];
}

- (id) initWithString:(NSString*)str dimensions:(CGSize)dimensions alignment:(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.isRelativeAnchorPoint = NO;

        int pos = 0;
        int i = 0;
        while (pos<[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 > dimensions.width || pos+end == [str length]) {
                    if (pos+end == [str length] && actualSize.width <= 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) alignment: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

Easy to use;) I have to finish the class with getters, setters and all that. I know this is a β€œdo-it-yourself” solution, but hey! He works!

+4
source

For those with Cocos 2d 2.x, I modified @Hardschool code to fix the obsolete methods and it works amazingly!

in .h

#import <Foundation/Foundation.h>
#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

in .m file

#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<[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 > dimensions.width || pos+end == [str length]) {
                if (pos+end == [str length] && actualSize.width <= 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

What is that, thanks @Hardschool!

0

gmogames, , , setColor

void CCLabelTTFLineSpaced::setColor(ccColor3B color)
{
    for (int i = 0; i < getChildren()->count(); i ++)
    {
        CCLabelTTF* child = (CCLabelTTF*)getChildren()->objectAtIndex(i);
        child->setColor(color);
    }
}
0

, , Cocos2d . Github.

I start with the @gmoagames (and @Hardschool) version, add a method setColor:from @Alex and add a method to reduce transparency.

Feel free to send me any merge requests if you have further improvements.

And many thanks for all the code that was distributed here.

0
source

All Articles