UIFontComplete

Build Status Swift 5.0 platforms CodeCov CocoaPods compatible Carthage compatible Swift Package Manager compatible License

iOS 和 tvOS 的字体管理 (系统 & 自定义)

使用方法

无需再浪费时间搜索 UIFont 字体的名称,也不用担心字体名称拼写错误导致运行时出现意外。这个库只是一个对 UIFont 的扩展和一个 Font enum,包含了 iOS 和 tvOS 上每个系统字体的 case。还支持自定义字体,请继续阅读了解详情!

无需再使用 UIFont 原生的基于 String 的构造函数

let font = UIFont(name: "Arial-BoldItalicMT", size: 12.0)

现在您只需开始输入字体枚举的名称,让代码补全帮助您

这个库目前提供了两种不同的方式来创建 UIFont 对象。第一种是从提供的 BuiltInFont 枚举中调用字体名称,然后调用 of(size:) 来提供所需的大小。

let myFont: UIFont? = BuiltInFont.helvetica.of(size: 12.0)

这个库提供的另一种 UIFont 创建方法类似于普通的 UIFont 构造函数,不同之处在于,它提供的是 Font 枚举的一个 case,而不是提供所需字体的 String

let font = UIFont(font: .arialBoldItalicMT, size: 12.0)

如何使用自定义字体?

自定义字体可以通过 FontRepresentable 协议添加。只需创建您自己的基于 StringCustomFont enum,并遵循 FontRepresentable 协议,并为您想要添加的每个字体添加一个 case,如下所示:

// In your project using custom fonts

enum CustomFont: String, FontRepresentable {
    case alexBrushRegular = "AlexBrush-Regular"
}

CustomFont.alexBrushRegular.of(size: 12.0) // => UIFont

SwiftUI 支持

SwiftUI Font 的处理方式与 UIKit UIFont 相同。

您可以同时使用这两种方式

let myFont: Font = BuiltInFont.helvetica.of(size: 12.0)
let font = Font(font: .arialBoldItalicMT, size: 12.0)

安装

Carthage

如果您使用 Carthage 来管理依赖项,只需将 UIFontComplete 添加到您的 Cartfile

github "Nirma/UIFontComplete"

如果您使用 Carthage 构建您的依赖项,请确保您已将 UIFontComplete.framework 添加到您的 target 的 "Linked Frameworks and Libraries" 部分,并将 UIFontComplete.framework 包含在您的 Carthage 框架复制构建阶段。

CocoaPods

如果您使用 CocoaPods 来管理依赖项,只需将 UIFontComplete 添加到您的 Podfile

pod 'UIFontComplete'

Swift Package Manager

在 Xcode 中,选择 File > Add Packages,然后将此存储库的 URL 复制粘贴到搜索字段,然后点击“Add Package”按钮。

要求

为 UIFontComplete 做出贡献

欢迎提交 Pull Request! 如果您认为这个库可以做得更好,请通过发送 pull request 来实现!

许可证

UIFontComplete 是免费软件,可以按照 LICENSE 文件中指定的条款进行重新分发。