我已经能够成功地使用 Xcode 12b1 和 Swift 5.3 在 Swift 包中提供一些图像和资产目录。在 Swift 包中使用自定义 .ttf 文件并没有那么幸运。
我在清单中加载一个 .ttf 文件,如下所示:
.target(
name: "BestPackage",
dependencies: [],
resources: [
.copy("Resources/Fonts/CustomFont.ttf"),
.process("Resources/Colors.xcassets")
]
),
Run Code Online (Sandbox Code Playgroud)
我注意到FontSwiftUI 中的类型没有初始化器来包含模块中的资产。例如,这有效:
static var PrimaryButtonBackgroundColor: SwiftUI.Color {
Color("Components/Button/Background", bundle: .module)
}
Run Code Online (Sandbox Code Playgroud)
但是,无法指定字体的来源。我希望将它加载到模块中会将它发送到目标中以供使用,但没有这样的运气:
static var PrimaryButtonFont: Font {
Font.custom("CustomFont", size: 34)
}
Run Code Online (Sandbox Code Playgroud)
这不会按预期加载字体。我正在研究使用 CoreText api 尝试诱使它加载,但我觉得应该有更简单的方法。有什么建议吗?
更新
仍然没有成功,但我能够证明字体确实在模块内
我编写了一个方法来从模块中获取可用的字体 URL,如下所示:
static func fontNames() -> [URL] {
let bundle = Bundle.module
let filenames = ["CustomFont"]
return filenames.map { bundle.url(forResource: $0, withExtension: "ttf")! } …Run Code Online (Sandbox Code Playgroud) 有没有人想出如何在Info.plistXcode 13 中添加/编辑值?我看到他们Info.plist从导航器窗格中移动了...但是虽然我可以找到它,但我不确定如何编辑它。