Swift 自定义字体 Xcode [英] Swift Custom Fonts Xcode

查看:31
本文介绍了Swift 自定义字体 Xcode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Swift 在 Xcode(版本 7.0 Beta)中创建一个游戏,我想在游戏结束时以gameOver.ttf"字体显示标签Game Over".我已将字体添加到我的资源文件夹中.我不知道如何在我的代码中引用它.我能得到帮助吗?我的代码:

I am creating a game in Xcode (Version 7.0 Beta) using Swift, and I would like to display the label "Game Over" at the end of the game in the font "gameOver.ttf". I have added the font to my resources folder. I do not know how to reference it in my code. Can I please get help? My Code:

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)

推荐答案

这些是为您的应用程序添加自定义字体的步骤:

These are the steps to add a custom font to you application:

  1. 在您的应用程序中添加gameOver.ttf"字体(确保它包含在目标中)
  2. 修改 application-info.plist 文件.
  3. 在新行中添加键应用程序提供的字体"
  4. 并在数组中添加gameOver.ttf"作为新项目应用程序提供的字体".

现在该字体将在 Interface Builder 中可用.要在代码中使用自定义字体,我们需要通过名称来引用它,但名称通常与字体的文件名不同

Now the font will be available in Interface Builder. To use the custom font in code we need to refer to it by name, but the name often isn’t the same as the font’s filename

有两种方法可以找到名字:

There are 2 ways to find the name:

  1. 在 Mac 上安装字体.打开字体书,打开字体看看列出的名字是什么.
  2. 以编程方式列出应用中的可用字体

对于第二种方法,添加这一行是您的应用程序委托的 didFinishLaunchingWithOptions

for the second approach add this line is your app delegate’s didFinishLaunchingWithOptions

print(UIFont.familyNames)

要在 Swift 5 中列出每个字体系列中包含的字体:

To list the fonts included in each font family, in Swift 5:

 func printFonts() {
    for familyName in UIFont.familyNames {
        print("
-- (familyName) 
")
        for fontName in UIFont.fontNames(forFamilyName: familyName) {
            print(fontName)
        }
    }
}

找到自定义字体的名称后,您可以像这样使用它:

after finding the name of your custom font you may use it like this:

SKLabelNode(fontNamed: "gameOver") // put here the correct font name

或在一个简单的标签中:

or in a simple label :

cell.textLabel?.font = UIFont(name: "gameOver", size: 16) // put here the correct font name 

有用资源

这篇关于Swift 自定义字体 Xcode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆