UIFontComplete
为 iOS 和 tvOS 提供系统与其自有字体的字体管理
用法
不再浪费时间搜索 UIFont
字体的名称,也不会在运行时因字体名称错误而出错。这个库对 UIFont
增加了一个扩展和一个 Font
枚举,该枚举有一个表示 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》协议添加自定义字体。只需创建自己的基于《FontRepresentable》的自定义《String》枚举,并针对要添加的每个字体添加一个情况,如下所示:
// In your project using custom fonts
enum CustomFont: String, FontRepresentable {
case alexBrushRegular = "AlexBrush-Regular"
}
CustomFont.alexBrushRegular.of(size: 12.0) // => UIFont
SwiftUI支持
SwiftUI中字体处理方式与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
添加到目标的“Linked Frameworks and Libraries”部分,并将其包括在你的Carthage框架复制构建阶段中。
CocoaPods
如果你使用CocoaPods来管理你的依赖项,只需将UIFontComplete添加到你的Podfile
中。
pod 'UIFontComplete'
Swift Package Manager
在 Xcode 中,选择文件 > 添加包,然后将此存储库的 URL 复制粘贴到搜索栏中,并点击“添加包”按钮。
要求
- Xcode 9.0
- Swift 4.0+
为 UIFontComplete 做贡献
欢迎 Pull Requests! 如果你觉得这个库可以改进,请通过发送 pull request 来实现!
许可
UIFontComplete 是免费软件,可以在 LICENSE 文件中指定的条款下重新分发。