Swift 包中的字体破坏了预览,为什么?

问题描述 投票:0回答:1

问题

添加到 swift 包中的字体似乎以某种方式破坏了我的预览/画布。应该注意的是,我正在使用非循环依赖图,这意味着几乎所有内容都被分成整齐的包,例如资源、设计、模型等。考虑到我收到此错误,这可能是相关的,存在于我的

Design
包中,而不是在我的项目的根目录中。

此外,它可以在真实设备或模拟器上构建和运行,没有任何问题,并且工作完美,这意味着它与预览加载字体的方式 100% 有关。我本以为它会故障转移到系统字体,但事实并非如此。

我尝试过的一个解决方案是这样做,但没有效果。

        if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" {
            return .system(size: size)
        }

错误

== PREVIEW UPDATE ERROR:

    LinkDylibError: Failed to build TextButtonStyle.swift
    
    Linking failed: linker command failed with exit code 1 (use -v to see invocation)
    
    ld: warning: search path '/Applications/Xcode.app/Contents/SharedFrameworks-iphonesimulator' not found
    ld: Undefined symbols:
      (extension in AppResources):SwiftUI.Font.h4.unsafeMutableAddressor : SwiftUI.Font, referenced from:
          (extension in Design_PreviewReplacement_TextButtonStyle_1):Design.TextButtonStyle.(__preview__makeBody in _8A299D844C46E27E8BBB012BB76349C4)(configuration: SwiftUI.ButtonStyleConfiguration) -> some in TextButtonStyle.1.preview-thunk.o
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    
    
    ==================================

再现

  • 将字体和字体+扩展添加到 Swift 包
  • 在字体扩展中,引用静态编程的字体。例如:
    public static var h1 = customFont(weight: "Bold", size: 22)
  • 将新包导入到项目中。
  • 创建一个视图,使用该字体,例如:
    Text("Hello, World").font(.h1)
  • 观察预览的破坏。

我的代码

我尽可能缩小了它的大小,同时保留了整个上下文。还有一个

UIFont
版本,但我没有将其包含在此处,因为这似乎不是我的问题的原因。

public struct FontConfig: Codable {
    public enum FontSet: String {
        case inter = "Inter"
        // Other Cases
    }

    public static var currentFontSet: FontSet = .nunito {
        didSet {
            Font.h1 = Font.customFont(weight: "Bold", size: 22)
            //Other Cases
        }
    }
}

// swiftlint:disable identifier_name
extension Font {
    public static var h1 = customFont(weight: "Bold", size: 22)
    // Other Fonts

    public static func customFont(weight: String, size: CGFloat) -> Font {
        if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" {
            return .system(size: size)
        }
        
        print(">> Font Name: \(FontConfig.currentFontSet.rawValue)-\(weight)")
        return Font.custom("\(FontConfig.currentFontSet.rawValue)-\(weight)", size: size)
    }
}

我也有这个

FontConfig+Extension
可以注册,但是在预览的上下文中永远不会被击中。

private static func registerFont(fontName: String) {
        let bundle = Bundle.module
        let pathForResourceString = bundle.path(forResource: fontName, ofType: "ttf")
        
        guard let path = pathForResourceString else {
            print("\(fontName) font not found")
            return
        }
        
        guard let fontData = NSData(contentsOfFile: path),
              let dataProvider = CGDataProvider(data: fontData),
              let fontRef = CGFont(dataProvider) else {
            print("\(fontName) can't be loaded")
            return
        }
        
        var errorRef: Unmanaged<CFError>? = nil
        if CTFontManagerRegisterGraphicsFont(fontRef, &errorRef) == false {
            print("Error registering font")
        }
        
        print("\(fontName) font loaded")
    }
ios swift swiftui fonts swiftui-previews
1个回答
0
投票

不幸的是,在这种情况下,您可能必须在每次预览时注册字体。公开一个执行此操作的方法并将其添加到宏的主体中。

#Preview {
    // The method which registers all custom fonts in the bundle
    MyFontsService.registerAppFonts()

    return MyView()
}
© www.soinside.com 2019 - 2024. All rights reserved.