ios Swift 自定义字体 Xcode 7

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31420758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:51:56  来源:igfitidea点击:

Swift Custom Fonts Xcode 7

iosxcodeswiftfonts

提问by theenigma

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:

我正在使用 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)

回答by Aladin

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

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

  1. Add "gameOver.ttf" font in your application ( Make sure that it's included in the target)
  2. Modify the application-info.plist file.
  3. Add the key "Fonts provided by application"in a new row
  4. and add "gameOver.ttf" as new item in the Array "Fonts provided by application".
  1. 在您的应用程序中添加“gameOver.ttf”字体(确保它包含在目标中
  2. 修改 application-info.plist 文件。
  3. 在新行中添加键“应用程序提供的字体”
  4. 并在数组“应用程序提供的字体”中添加“gameOver.ttf”作为新项目。

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

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

There are 2 ways to find the name:

  1. Install the font on your Mac. Open Font Book, open the font and see what name is listed.
  2. Programmatically list the available fonts in your app

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

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

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

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

println(UIFont.familyNames())

To list the fonts included in each font family, in swift 2:

要列出每个字体系列中包含的字体,请在 swift 2 中:

class func printFonts() {
    for familyName in UIFont.familyNames() {
        print("\n-- \(familyName) \n")
        for fontName in UIFont.fontNamesForFamilyName(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 

Useful resource

有用的资源

回答by Gurjinder Singh

Swift 4 and 5.I have created an enum for App fonts. First install fonts on system by double click on desired font. Then installed font will appear under Custom fonts in Attribute inspector.

Swift 4 和 5。我为 App 字体创建了一个枚举。首先通过双击所需的字体在系统上安装字体。然后安装的字体将出现在属性检查器中的自定义字体下。

enter image description here

在此处输入图片说明

import Foundation
import UIKit

private let familyName = "Montserrat"

enum AppFont: String {
    case light = "Light"
    case regular = "Regular"
    case bold = "Bold"

    func size(_ size: CGFloat) -> UIFont {
        if let font = UIFont(name: fullFontName, size: size + 1.0) {
            return font
        }
        fatalError("Font '\(fullFontName)' does not exist.")
    }
    fileprivate var fullFontName: String {
        return rawValue.isEmpty ? familyName : familyName + "-" + rawValue
    }
}

Usage

用法

self.titleLabel?.font = AppFont.regular.size(12.0)

回答by Suresh Maidaragi

Along with the above answer, I would like to add an another answer on installing the custom fonts to Xcode which can be access to Mac as well as MAC.

除了上面的答案,我想添加另一个关于将自定义字体安装到 Xcode 的答案,它可以访问 Mac 和 MAC。

1. Select and download your favourite font styles from here

1. 从这里选择并下载您喜欢的字体样式

2. Unzip the download folder and select all the fonts from folder and double tap to open

3. Pop up appears to select all the fonts > check all and install

4. Open Xcode, one can see from Attribute inspector for selected label

That's all :)

就这样 :)

Watch out the attached pic for more ref:

请注意附加图片以获取更多参考:

enter image description here

在此处输入图片说明

Font Lato appeared in Xcode workspace

字体 Lato 出现在 Xcode 工作区中

enter image description here

在此处输入图片说明