Elefont 1.0.2

Elefont 1.0.2

Mihai Cristian Tanase 维护。



Elefont 1.0.2

  • Mihai Cristian Tanase

Swift Support Platform CocoaPods


如果您

  • 想要在 iOS 项目中轻松添加和使用自定义字体
    或者
  • 您想从自定义 Bundle(s) 中加载数字
    或者
  • 您想加载和使用已与应用程序捆绑的字体以外的字体

那么 Elefont 就是您所需要的。

  • Bundle.main 导入字体
  • 从自定义 Bundle(s) 导入字体
  • 从任何本地 URL 或路径导入字体
  • CocoaPods 支持

CocoaPods

pod 'Elefont'

手册

  1. 下载Elefont.
  2. Sources/Elefont.swift 复制到您的项目中。

用法

没有使用Elefont的典型自定义字体集成通常如下

  1. 将您的字体添加到Xcode项目中;
  2. 创建一个包含这些字体的列表并更新 Info.plist 文件;
  3. Storyboard/Xib 中使用字体或通过以下方式编程使用:
    UIFont(name: "<font-postscript-name>", size: <size>)

如果您在iOS项目中集成了自定义字体(我假设您已经这样做了,因为您在寻找更好的解决方案),那么您知道步骤 2(例如,字体文件名称错误)和步骤 3(错误的字体PostScript名称,这 不是 字体文件名称)是多么麻烦。

幸运的是,Elefont通过这一行解决了这两个烦恼

Elefont.eat()

这将加载 Bundle.main 中的所有字体,完全消除步骤 2,并提供已加载字体的PostScript名称。

但如果您的字体在一个自定义的Bundle中呢?没问题

Elefont.eat(bundle: <custom_bundle_object>)

我之前提到过,Elefont可以从本地URL或路径加载字体。下面是如何做到这一点的示例

Elefont.eat(at:)
// or
Elefont.eat(atPath:)

上述每种方法都可以与完成处理程序一起调用,以防您需要由加载操作生成的所有已加载字体的列表

Elefont.eat(/* arguments */) { fonts in
  print(fonts)
}

如果您需要调试字体加载,设置 debugEnabled = true

Elefont.debugEnabled = true

公共API

以下是Elefont使用的所有可能变体(fonts 是包含PostScript字体名称的字符串数组)

// 1. Load from main Bundle without callback
Elefont.eat()

// 2. Load from main Bundle with callback
Elefont.eat { fonts in
  print(fonts)
}

// 3. Load from custom Bundle without callback
Elefont.eat(bundle:)

// 4. Load from custom Bundle with callback
Elefont.eat(bundle:) { fonts in
  print(fonts)
}

// 5. Load from local URL without callback
Elefont.eat(at:)

// 6. Load from local URL with callback
Elefont.eat(at:) { fonts in
  print(fonts)
}

// 7. Load from local path without callback
Elefont.eat(atPath:)

// 8. Load from local path with callback
Elefont.eat(atPath:) { fonts in
  print(fonts)
}

示例项目

您可以使用ElefontDemo/ElefontDemo.xcodeproj项目来查看Elefont如何从主Bundle和URL路径中加载字体。按下“加载字体”按钮,可以查看文本更新为其相应的字体。

由...

Mihai Cristian Tanase