mtpFontManager 2.0.4

mtpFontManager 2.0.4

测试已测试
语言语言 SwiftSwift
许可 MIT
发布上次发布2019 年 4 月
SPM支持 SPM

mtpFontManager 维护。




  • 作者:
  • Mostafa Taghipour

mtpFontManager

CI Status Version License Platform

Android 版本在此

mtpFontManger 是一个 iOS 字体管理器

  • 支持自定义字体
  • 适用于整个应用
  • 支持多字体
  • 运行时更换字体
  • 兼容界面构建器
  • 支持各种样式和权重
  • 支持动态类型

custom font app

系统要求

  • iOS 8.2+
  • Xcode 9+

安装

mtpFontManager 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中

pod 'mtpFontManager'

用法

  1. 将自定义字体添加到您的项目中

fonts

  1. 注册您的字体

    方法1:在 Info.plist 中注册

    在将字体文件添加到您的项目中后,您需要让 iOS 知道这个字体。为此,您需要在 Info.plist 中添加“由应用提供字体”键(原始键名为 UIAppFonts)。Xcode 为该键创建了一个数组值;将字体文件的名称添加到数组中作为一项。请确保将文件扩展名作为名称的一部分。 plist

    方法2:通过代码加载自定义字体

    为此使用 FontBlaster 库

    首先添加 FontBlaster 库

    pod 'FontBlaster'

    然后使用以下代码加载项目中添加的字体

    FontBlaster.blast()
  2. 声明字体

lazy var exo: AppFont = {
    let font = AppFont(
        id: 1,
        familyName: "Exo",
        defaultFont: "Exo-Regular",
        ultraLight: "Exo-Thin",
        thin: "Exo-ExtraLight",
        light: "Exo-Light",
        regular: "Exo-Regular",
        medium: "Exo-Medium",
        semibold: "Exo-Semibold",
        bold: "Exo-Bold",
        heavy: "Exo-ExtraBold",
        black: "Exo-Black"
    )
    return font
}()

在 plist 文件中声明字体

除了上述方法外,您还可以通过使用 plist 文件来定义字体。为此,创建一个新的 plist 文件并声明您的字体,不同权重: plist

然后使用 AppFont plist 构造器

lazy var taviraj: AppFont = {
	let font = AppFont(plist: "taviraj" /* plist file name */)
	return font
}()
  1. 以通常的方式使用字体

    界面构建器: use storyboard

    或代码方式

    label.font=UIFont.preferredFont(forTextStyle: .body)
    label2.font=UIFont.boldSystemFont(ofSize: 17.0)

动态类型

如果您想使用动态类型,请在您的视图控制器中声明StyleWatcher,并监视使用动态字体的视图,如下所示:

import UIKit
import mtpFontManager

class ViewController: UIViewController {
    @IBOutlet weak var label: UILabel!

    let watcher = StyleWatcher()

    override func viewDidLoad() {
        super.viewDidLoad()

        //if you want use dynamic types programmically, you must declare it before watch views
        label.font=UIFont.preferredFont(forTextStyle: .body)

        //whatch view that include the controls that use dynamic types
        watcher.watchViews(inView: view)

        //Or you can just watch spececific control that use dynamic types
        /*
            watcher.watchLabel(label: label)
            watcher.watchButton(label: button)
            watcher.watchTextField(label: textField)
            watcher.watchTextView(label: textView)
        */
    }
}
  1. 每当您需要更改应用程序的字体时,请使用以下代码:
FontManager.shared.currentFont = taviraj /* your desired font */
  1. 就这样,享受这个过程吧!

示例

要运行示例项目,请首先将其克隆到本地仓库,然后在 Example 目录中运行 pod install

灵感

本项目受到了 Gliphy 的极大启发。向 @Tallwave 致敬。👍

作者

Mostafa Taghipour, [email protected]

许可

mtpFontManager 项目在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。