CCLabelTTF 中的行间距

发布于 2024-12-11 07:47:05 字数 50 浏览 0 评论 0原文

ios应用程序是否有办法调整cocos2d中CCLabelTTF中多行之间的行距?谢谢

Is there anyway in ios app to adjust the linespacing between the multiple lines in CCLabelTTF in cocos2d? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蹲在坟头点根烟 2024-12-18 07:47:05

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

这是 .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

这是 .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

易于使用;)我必须使用 getter、setter 和所有内容来完成该课程。我知道这是一个“自制”解决方案,但是嘿!有用!

The answer to you question is no. You can't adjust a CCLabelTTF linespacing. But hey! I will share with you my solution for this ;)

This is the .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 is the .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 complete the class with getters, setters and all stuff. I know that this is a "homemade" solution, but hey! It works!

蓬勃野心 2024-12-18 07:47:05

对于使用 Cocos 2d 2.x 的用户,我更改了 @Hardschool 代码来修复已弃用的方法,并且效果非常好!

.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

在.m 文件的

#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!

For those with Cocos 2d 2.x, I changed @Hardschool code to fix the deprecated methods and it's working awesome!

in the .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 the .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

That's it, thanks @Hardschool!

渔村楼浪 2024-12-18 07:47:05

gmogames,这里以setColor为例

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

gmogames, here is setColor for example

void CCLabelTTFLineSpaced::setColor(ccColor3B color)
{
    for (int i = 0; i < getChildren()->count(); i ++)
    {
        CCLabelTTF* child = (CCLabelTTF*)getChildren()->objectAtIndex(i);
        child->setColor(color);
    }
}
余厌 2024-12-18 07:47:05

我认为这是许多 Cocos2d 开发人员偶然发现的一个问题。因此,为了提高此类的可读性和协作性,我在 Github 上为其创建了一个存储库。

这是链接

我从@gmoagames(和@Hardschool)的版本开始,添加了setColor: 来自 @Alex 的方法,并添加了一个方法来淡化不透明度。

如果您有更多改进,请随时向我发送任何合并请求。

非常感谢这里分享的所有代码。

I think this is an Issue were many Cocos2d Developers stumbled upon. So to improve readability and collaboration on this class I was creating an repository on Github for it.

Here is the link

I startet at the Version of @gmoagames (and @Hardschool), added the setColor: Method from @Alex and added a Method to fade the opacity.

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文