gpt4 book ai didi

ios - 如何更改 Xcode Swift Playgrounds(.swiftpm) 项目中的字体?

转载 作者:行者123 更新时间:2023-12-05 05:42:57 24 4
gpt4 key购买 nike

如何在新的 Xcode Swift Playgrounds 项目中实现自定义字体?在简单的 Xcode App 项目中,我们导入字体并将其添加到 info plist(应用程序提供的字体)中,但是在此类项目中没有 info plist,我该怎么办?

最佳答案

我在 friend 的帮助下自己找到了解决方案,我必须创建一个带有 fileprivate 静态函数的公共(public)结构 MyFont,然后在主文件中的 var 主体之前添加一个 init。

这是代码:

我的字体

import SwiftUI

public struct MyFont {
public static func registerFonts() {
registerFont(bundle: Bundle.main , fontName: "YOUR-FONT-HERE", fontExtension: ".ttf") //change according to your ext.
}
fileprivate static func registerFont(bundle: Bundle, fontName: String, fontExtension: String) {

guard let fontURL = bundle.url(forResource: fontName, withExtension: fontExtension),
let fontDataProvider = CGDataProvider(url: fontURL as CFURL),
let font = CGFont(fontDataProvider) else {
fatalError("Couldn't create font from data")
}

var error: Unmanaged<CFError>?

CTFontManagerRegisterGraphicsFont(font, &error)
}
}

我的应用

@main
struct MyApp: App {

//add the init before var body
init() {
MyFont.registerFonts()
}

var body: some Scene {
//
}
}

关于ios - 如何更改 Xcode Swift Playgrounds(.swiftpm) 项目中的字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71916171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com