【问题标题】:Swift Custom Fonts XcodeSwift 自定义字体 Xcode
【发布时间】:2015-10-03 22:11:14
【问题描述】:

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

【问题讨论】:

标签: ios xcode swift fonts


【解决方案1】:

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

  1. 在您的应用程序中添加“gameOver.ttf”字体 (Make sure that it's included in the target)
  2. 修改application-info.plist文件。
  3. 在新行中添加键“应用程序提供的字体”
  4. 并在数组中添加“gameOver.ttf”作为新项目“应用程序提供的字体”

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

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

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

对于第二种方法,添加这一行是您的应用代理的didFinishLaunchingWithOptions

print(UIFont.familyNames)

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

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

Useful resource

【讨论】:

  • OMG..这是我丢失的链接 - 我没有将自定义字体添加到 Info.plist 谢谢!你让我开心!
【解决方案2】:

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

1. Select and download your favourite font styles from here

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

就是这样:)

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

Font Lato 出现在 Xcode 工作区中

【讨论】:

    【解决方案3】:

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

    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
        }
    }
    

    用法

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

    【讨论】:

      【解决方案4】:

      以防万一您需要将其添加到您的应用程序中,您还必须关闭 XCode 并再次打开它,以便在将新添加的字体添加到 info.plist 后,新添加的字体可以显示在字体下拉列表中。

      在这里查看他们的官方文档:https://developer.apple.com/documentation/uikit/text_display_and_fonts/adding_a_custom_font_to_your_app

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-14
        • 2014-10-07
        • 1970-01-01
        • 1970-01-01
        • 2013-09-27
        • 2021-04-03
        • 2012-05-19
        相关资源
        最近更新 更多