我在SKLabelNode中显示多行标签时遇到了一些问题。首先,我知道没有lines属性或类似的任何东西。我知道有一些GitHub项目可以完成,但我宁愿不使用它们。
我之所以需要多行代码,是因为我在应用程序中的采购方式。有时候,描述不可能都是一字一句。例如,在最大的DynamicType标题字体大小下呈现的字符串:
Cheat and get 25% of the points needed for the "Chaos" theme!不适合任何iOS手机上的肖像布局。我试着调用这个函数:
-(NSString*)autoInsertLineBreaksInString:(NSString*)string {
    NSLog(@"Function called with %@", string);
    NSMutableArray* arrayOfSpaceIndexes = [NSMutableArray array];
    NSMutableString* returnString = [string mutableCopy];
    for (int i = 0; i < string.length; i++) {
        if ([string characterAtIndex:i] == [@" " characterAtIndex:0]) {
            NSLog(@"Adding %i to array of space indexes.", i);
            [arrayOfSpaceIndexes addObject:@(i)];
        } else {
            NSLog(@"Not adding %i to array of space indexes.", i);
        }
    }
    [returnString replaceCharactersInRange:NSMakeRange([[arrayOfSpaceIndexes objectAtIndex:(NSUInteger)(floorf((arrayOfSpaceIndexes.count - 1) / 2))] unsignedIntegerValue], 1) withString:@"\n"];
    NSLog(@"Retruning %@", returnString);
    return returnString;
}它在正确的位置插入\n并输出这个.
Cheat and get 25% of the
points needed for the "Chaos" theme....but标签节点将\n解释为空格,给出原始字符串。我真的不想使用第二个标签节点。我有什么办法解决这个问题吗?
发布于 2015-07-12 17:18:33
将注释形式化为一个答案:如果您选择使用多个SKLabelNode实例,您可以创建一个包装器SKNode并将每个SKLabelNode添加到包装器中。当您将包装器添加到场景中时,只需管理一个包装器,就可以隐藏/显示/移动/等所有标签节点。
发布于 2016-10-04 20:10:06
这条线帮助我走上了正确的方向。以下是我建议的解决方案的目标-c实现:
NSInteger fontSize = 40;
SKNode *messageNode;
messageNode = [[SKNode alloc] init];
NSArray *components = [messageString componentsSeparatedByString:@"\n"];
for(int i = 0; i < [components count]; i++){
    SKLabelNode *subNode = [SKLabelNode labelNodeWithFontNamed:@"Helvetica-Bold"];
    subNode.text = [components objectAtIndex:i];
    subNode.position = CGPointMake(0, -fontSize*i);
    subNode.fontSize = fontSize;
    subNode.fontColor = [SKColor blackColor];
    subNode.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
    [messageNode addChild:subNode];
}注意,由于每条新行都是相对于父节点垂直向下移动的,所以当您将messageNode放置在场景中时,位置将基于第一行文本的中心点,而不是整个文本块的中心点。这可以很容易地通过调整messageNode的中心点向下调整,以使之下降。请参阅下面的背景代码中的backgroundNode.position行。
我还编写了一些代码来创建文本块的背景:
    //Get widest child frame
    NSInteger frameWidth = 0;
    for(int i = 0; i < [messageNode.children count]; i++){
        if([messageNode.children objectAtIndex:i].frame.size.width > frameWidth){
            frameWidth = [messageNode.children objectAtIndex:i].frame.size.width;
        }
    }
    NSInteger singleLineHeight = [messageNode.children objectAtIndex:0].frame.size.height;
    NSInteger lineCount = [messageNode.children count];
    NSInteger totalFrameHeight = singleLineHeight * lineCount;
    CGSize backgroundFrameSize = CGSizeMake(frameWidth * 1.1, totalFrameHeight * 1.5);
    CGFloat cornerRadius = 10;
    SKShapeNode *backgroundNode = [SKShapeNode shapeNodeWithRectOfSize:backgroundFrameSize cornerRadius:cornerRadius];
    //Bump the frame down so it is vertically centered around all lines of text, rather than vertically centered on the first line.
    backgroundNode.position = CGPointMake(0, -(totalFrameHeight/2 - singleLineHeight/2) );
    backgroundNode.zPosition = -1;
    [messageNode addChild:backgroundNode];https://stackoverflow.com/questions/31351798
复制相似问题