Objective c CCLabelTTF中的行距
ios应用程序中是否有调整cocos2d中CCLabelTTF中多行之间的行距的方法?谢谢您的问题的答案是否定的。您不能调整CCLabelTTF行距。但是嘿!我将与您分享我的解决方案;) 这是hObjective c CCLabelTTF中的行距,objective-c,ios,cocos2d-iphone,Objective C,Ios,Cocos2d Iphone,ios应用程序中是否有调整cocos2d中CCLabelTTF中多行之间的行距的方法?谢谢您的问题的答案是否定的。您不能调整CCLabelTTF行距。但是嘿!我将与您分享我的解决方案;) 这是h #import <Foundation/Foundation.h> #import "cocos2d.h" @interface CCLabelTTFLineSpaced : CCLayer { } + (id) labelWithString:(NSString*)string dim
#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
#导入
#导入“cocos2d.h”
@接口CCLABELTTFLINESPACTED:CCLayer{
}
+(id)labelWithString:(NSString*)字符串尺寸:(CGSize)尺寸对齐:(CCTextAlignment)对齐fontName:(NSString*)名称fontSize:(CGFloat)大小线空间:(CGFloat)空间;
-(id)initWithString:(NSString*)str维度:(CGSize)维度对齐:(CCTextAlignment)对齐fontName:(NSString*)名称fontSize:(CGFloat)大小lineSpace:(CGFloat)空格;
@结束
这是我的
#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
#导入“CCLabelTTFLineSpaced.h”
@实现CCLabelTTFLineSpaced
+(id)labelWithString:(NSString*)字符串尺寸:(CGSize)尺寸对齐:(CCTextAlignment)对齐fontName:(NSString*)名称fontSize:(CGFloat)大小线空间:(CGFloat)空间{
return[[[self alloc]initWithString:string维度:维度对齐:对齐fontName:name fontSize:size lineSpace:(CGFloat)space]自动释放];
}
-(id)initWithString:(NSString*)str维度:(CGSize)维度对齐:(CCTextAlignment)对齐fontName:(NSString*)名称fontSize:(CGFloat)大小线空间:(CGFloat)空间{
if((self=[super init])){
锚定点=ccp(0.5f,0.5f);
[自我设置内容大小:维度];
self.isRelativeAnchorPoint=否;
int pos=0;
int i=0;
while(pos dimensions.width | | pos+end==[str length]){
如果(pos+end==[str length]&&actualSize.width对于那些使用cocos2d2.x的人,我更改了@Hardschool代码来修复不推荐的方法,它的工作非常棒
在
#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
#导入
#导入“cocos2d.h”
@接口CCLABELTTFLINESPACTED:CCLayer{
}
+(id)labelWithString:(NSString*)字符串尺寸:(CGSize)尺寸hAlignment:(CCTextAlignment)对齐fontName:(NSString*)名称fontSize:(CGFloat)大小lineSpace:(CGFloat)空格;
-(id)initWithString:(NSString*)str dimensions:(CGSize)dimensions hAlignment:(CCTextAlignment)alignment fontName:(NSString*)name fontSize:(CGFloat)size lineSpace:(CGFloat)space;
@结束
在.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
#导入“CCLabelTTFLineSpaced.h”
@实现CCLabelTTFLineSpaced
+(id)labelWithString:(NSString*)字符串尺寸:(CGSize)尺寸hAlignment:(CCTextAlignment)对齐fontName:(NSString*)名称fontSize:(CGFloat)大小lineSpace:(CGFloat)空格{
return[[[self alloc]initWithString:string维度:dimensions hAlignment:alignment fontName:name fontSize:size lineSpace:(CGFloat)space]autorelease];
}
-(id)initWithString:(NSString*)str维度:(CGSize)维度hAlignment:(CCTextAlignment)对齐fontName:(NSString*)名称fontSize:(CGFloat)大小lineSpace:(CGFloat)空格{
if((self=[super init])){
锚定点=ccp(0.5f,0.5f);
[自我设置内容大小:维度];
self.ignoreanchortForPosition=是;
int pos=0;
int i=0;
while(pos dimensions.width | | pos+end==[str length]){
如果(pos+end==[str length]&&actualSize.widthgmogames,下面是setColor示例
void CCLabelTTFLineSpaced::setColor(ccColor3B color)
{
for (int i = 0; i < getChildren()->count(); i ++)
{
CCLabelTTF* child = (CCLabelTTF*)getChildren()->objectAtIndex(i);
child->setColor(color);
}
}
void CCLabelTTFLineSpaced::setColor(ccColor3B颜色)
{
对于(int i=0;icount();i++)
{
cclabeltf*child=(cclabeltf*)getChildren()->objectAtIndex(i);
子->设置颜色(颜色);
}
}
我认为这是许多Cocos2d开发人员偶然发现的问题。因此,为了提高此类的可读性和协作性,我在Github上为其创建了一个存储库
我从@gmoagames(和@Hardschool)的版本开始,添加了@Alex中的setColor:
方法,并添加了一个褪色不透明度的方法
如果您有更多改进,请随时向我发送任何合并请求
非常感谢这里分享的所有代码。知道如何设置此标签的位置(包括换行符)吗?不幸的是,此解决方案不允许您更改属性,例如颜色,甚至文本。需要进行修改才能正常工作。@gmoagames:感谢提供代码片段,您能告诉我如何避免在iOS4及更高版本上显示不同的文本吗(对于较高的版本可以,但对于低于5的版本,我在正确显示时遇到一些问题)。