FontBlaster

以编程方式将自定义字体加载到你的 iOS、macOS 和 tvOS 应用程序中。

Swift Support Platform CocoaPods SwiftPM Compatible


关于

告别通过属性列表导入自定义字体的方式,FontBlaster只需一行代码即可自动导入和加载应用程序 Bundle 中的所有字体。

特性

安装说明

Swift 版本 分支名称 是否会继续接收更新?
5.1+ master
5.0 swift5.0
4.2 swift4.2
4.1 swift4.1
3.2 swift3.2
3.1 swift3.1

CocoaPods

pod 'FontBlaster' # Swift 5.1+
pod 'FontBlaster', :git => 'https://github.com/ArtSabintsev/FontBlaster.git', :branch => 'swift5.0' # Swift 5.0
pod 'FontBlaster', :git => 'https://github.com/ArtSabintsev/FontBlaster.git', :branch => 'swift4.2' # Swift 4.2
pod 'FontBlaster', :git => 'https://github.com/ArtSabintsev/FontBlaster.git', :branch => 'swift4.1' # Swift 4.1
pod 'FontBlaster', :git => 'https://github.com/ArtSabintsev/FontBlaster.git', :branch => 'swift3.2' # Swift 3.2
pod 'FontBlaster', :git => 'https://github.com/ArtSabintsev/FontBlaster.git', :branch => 'swift3.2' # Swift 3.1

Swift Package Manager

.Package(url: "https://github.com/ArtSabintsev/FontBlaster.git", majorVersion: 4)

手动

  1. 下载 FontBlaster.
  2. FontBlaster.swift 复制到你的项目中。

设置

通常,所有字体都会自动在 Bundle.main 中找到。 即使你有一个自定义的 bundle,它通常也会位于 mainBundle 内部。 因此,要加载应用程序中的所有字体,无论它位于哪个 bundle 中,只需调用

FontBlaster.blast() // Defaults to Bundle.main if no arguments are passed

如果你要从 app 的 mainBundle 中找不到的 bundle 加载字体,只需在 blast(_:) 方法中传递对你的 Bundle 的引用

FontBlaster.blast(bundle:) // Takes one argument of type Bundle, or as mentioned above, defaults to Bundle.main if no arguments are passed

如果你需要所有已加载字体的列表,blast(_:) 方法的重载版本有一个完成处理程序,它会返回该列表。 与原始方法一样,此方法接受自定义 Bundle 或默认为 Bundle.main(如果未传递任何参数)。

// Defaults to Bundle.main as no argument is passed
FontBlaster.blast() { (fonts) in
  print(fonts) // fonts is an array of Strings containing font names
}

// Custom bundle is passed as argument
FontBlaster.blast(bundle:) { (fonts) in
  print(fonts) // fonts is an array of Strings containing font names
}

要打开控制台调试语句,只需在调用任何 blast() 方法**之前**设置 debugEnabled() = true

FontBlaster.debugEnabled = true
FontBlaster.blast()

示例项目

仓库中包含一个示例 iOS 项目。 当你启动应用程序时,所有字体都配置为加载自定义字体,但直到你按下导航栏上的按钮才会实际显示它们。 按下按钮后,FontBlaster 会导入你的字体并重新绘制视图。

灵感来源

这个项目建立在 Marco Arment 在他的 博客 上提出和撰写的一个旧解决方案的基础上。

创建和维护者

Arthur Ariel Sabintsev