- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 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.
...但是标签节点将 \n
解释为一个空格,给我原始字符串。我真的不想使用第二个标签节点。关于如何解决此问题的任何想法?
最佳答案
这个帖子帮助我找到了正确的方向。这是我对建议解决方案的 objective-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
放置在场景中时,位置将基于第一行文本的中心点, 而不是整个文本 block 的中心点。这可以通过将 messageNode 的中心点向下调整 totalHeight/2 - lineHeight/2 来轻松解决。请参阅下面后台代码中的 backgroundNode.position
行。
我也写了一些代码来为文本 block 创建背景:
//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];
关于ios - SKLabelNode 多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31351798/
新类总是在设备 iOS 9 上崩溃,并且 NewLabel 上的访问错误(文本:“”)。 为什么? import SpriteKit class NewLabel : SKLabelNode { ov
根据用户的位置来交换字体的好方法是什么。例如,我有“Play”一词,但如果用户在日本,则需要使用其他字体在屏幕上显示“プレイ”。到目前为止,我有这个要显示文本按钮: self.restartLabel
有人可以告诉我如何使 SKLabelNode 字体大小适应所有屏幕尺寸。我的意思不是先检查设备之类的答案,例如如果 UIDevice.... 如果您使用 SpriteKit 游戏模板创建新的 Xcod
我正在尝试更新 coinLabel,以便当点击“life”节点时,它将更新 SKLabelNode 并在从总数中减去硬币后显示正确的硬币。我是编码新手,所以如果还有其他问题,请回复!谢谢! impor
我正在尝试找出异常行为的原因。我的 SpriteKit 场景有一个 SKLabelNode 作为属性。 SKLabelNode 在名为 levelSelected 的方法中初始化: -(void)l
我在 SKLabelNode 中显示多行标签时遇到了一些麻烦。首先,我知道没有 lines 属性或任何类似的东西。我知道那里有 GitHub 项目可能能够做到这一点,但我宁愿干脆不使用它们。 我需要多
我正在使用 swift 在 xcode 6 中开发一款游戏。我有一个得分标签 var scoreLabel: SKLabelNode!。现在,在我的一种方法中,我显示了我的标签: scoreLabel
是否可以将 SKLabelNode 上的字体大小设置为某个动态值,以便它是最大允许值,以便可以显示整个文本。 最佳答案 每当标签被更新时,只需调用方法:adjustLabel。在那个 - (void)
我看到了this question ,但是并没有一个确定的解决问题的方法,也不完全一样。我试图简单地添加一个带有一些文本的 SKLabelNode 。我从来没有遇到过问题,但由于某种原因,这种情况正在
我正在使用 SpriteKit 制作游戏。 我正在尝试添加一个 SKLabelNode (pointsLabel),如果我不设置 label.isHidden = true,它就可以正常工作。但是,我
我正在使用 SpriteKit 的 框架制作游戏。我如何使用 SKLabelNodes 制作轮廓文本?似乎没有这样做的内置功能。如果在 SpriteKit 中没有办法做到这一点,我还有什么其他方法可以
在 SpriteKit 中,您现在可以设置 SKLabelNode 的行数,因此标签可能会经过几行。 let lb = SKLabelNode(fontNamed: "Courier-bold") l
我无法将文本设置为 SKLabelNode。知道为什么会这样吗? 我试过这样做: let label = childNode(named:"ScoreLabel") label.text = "Sco
我正在用 swift 编写一个 spriteKit 程序,并希望在屏幕的一角有一个分数标签,以便用户可以监控他们的分数。我有以下代码: var scoreLabel = SKLabelNode() .
我有 SKLabelNode 代表当前分数,每当分数值达到某个特定数字(例如 10、20 ......)时,我试图用增长和收缩序列制作动画。问题是动画本身有某种延迟,并且花费的时间比我设置的持续时间长
我有一组 SKLabelNodes。每个节点代表一个单词的一个字母。每个节点都是 SKSpriteNode 的子节点。 spriteNode 有一个显式设置的高度([text sizeWithAttr
我知道我可以使用这段代码创建自定义字体: [UIFont fontWithName:@"bebas-neue" size:10]; 但是如果我想创建一个 SKLabelNode,我该如何将其设置为字体
我有这个代码: var labelNivel = SKLabelNode() func anadirLabelLevel() { labelNivel.name = "labelNivel"
我正在制作一个场景,其中在 spriteKit 场景的底部逐行显示在场景背景中播放的歌曲的歌词。我尝试了多种不同的解决方案,但出于某种原因,每种解决方案中的一些歌词都没有显示或不同步。我试图使用 SK
我试图让 SKLabelNode 显示在我的屏幕上,但它没有显示。这是我的代码... override func didMoveToView(view: SKView) { let text
我是一名优秀的程序员,十分优秀!