首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CCLabelTTF中的行距

CCLabelTTF中的行距
EN

Stack Overflow用户
提问于 2011-10-22 09:27:30
回答 4查看 4.6K关注 0票数 5

在cocos2d中,ios应用程序有没有调整CCLabelTTF中多行之间的行距的方法?谢谢

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-12-02 07:02:43

你的问题的答案是否定的。您不能调整CCLabelTTF行间距。但是嘿!我将与您分享我的解决方案;)

这是.h

代码语言:javascript
复制
#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

这是.m

代码语言:javascript
复制
#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

易于使用;)我必须用getter、setter和所有的东西来完成这个类。我知道这是一个“自制”的解决方案,但是嘿!它起作用了!

票数 4
EN

Stack Overflow用户

发布于 2012-10-03 00:33:18

对于那些使用Cocos 2d 2.x的人,我更改了@Hardschool代码来修复不推荐使用的方法,它工作起来棒极了!

在.h中

代码语言:javascript
复制
#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

在.m文件中

代码语言:javascript
复制
#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

就是这样,谢谢@Hardschool!

票数 0
EN

Stack Overflow用户

发布于 2012-10-18 17:35:26

gmogames,下面是setColor示例

代码语言:javascript
复制
void CCLabelTTFLineSpaced::setColor(ccColor3B color)
{
    for (int i = 0; i < getChildren()->count(); i ++)
    {
        CCLabelTTF* child = (CCLabelTTF*)getChildren()->objectAtIndex(i);
        child->setColor(color);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7856833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档