我正在使用Swift在Xcode(7.0版Beta)中创建一个游戏,我想在游戏末尾以“gameOver.ttf”的字体显示标签“Game Over”。我已将字体添加到我的资源文件夹。我不知道如何在我的代码中引用它。我可以帮忙吗?
我的代码:
我的代码:
let label = SKLabelNode(fontNamed: "gameOver") label.text = "Game Over" label.fontColor = SKColor.redColor() label.fontSize = 150 label.position = CGPointMake(0,100) label.horizontalAlignmentMode = .Center node.addChild(label)
解决方法
这些是向您的应用程序添加自定义字体的步骤:
>在应用程序中添加“gameOver.ttf”字体(Make sure that it’s included in the target)
>修改application-info.plist文件。
>在新行中添加“应用程序提供的字体”键
>并将“gameOver.ttf”添加为数组“应用程序提供的字体”中的新项目。
现在,Interface Builder中将显示该字体。
要在代码中使用自定义字体,我们需要通过名称引用它,但是名称通常与字体的文件名不同
There are 2 ways to find the name:
- Install the font on your Mac. Open Font Book,open the font and see
what name is listed.- Programmatically list the available fonts in your app
对于第二种方法添加这一行是你的应用程序代理的didFinishLaunchingWithOptions
println(UIFont.familyNames())
要列出每个字体系列中包含的字体,在swift 2中:
class func printFonts() { for familyName in UIFont.familyNames() { print("\n-- \(familyName) \n") for fontName in UIFont.fontNamesForFamilyName(familyName) { print(fontName) } } }
找到您的自定义字体的名称后,您可以使用它:
SKLabelNode(fontNamed: "gameOver") // put here the correct font name
或简单的标签:
cell.textLabel?.font = UIFont(name: "gameOver",size: 16) // put here the correct font name
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。