1

我正在尝试使用摘录https://github.com/davedelong/sfsymbols提供的以下代码示例从 SF 符号前面进行字形处理。我已经隔离了无法使用从运行时提取的值的代码。

我已将代码压缩为仅产生问题所需的语句。

这个问题似乎是我在 CTFontGetGlyphWithName 中为最终语句提供的名称,我尝试的每个值组合都返回 0。

请注意,您必须安装 SF Symbols 应用程序才能获得初始捆绑包。

请原谅代码质量,这只是为了创建尽可能短的代码示例来重现问题。

谢谢。

我已经遵循了相当多的剪辑和教程,但似乎没有很多可用的这些更高级功能的实际示例。

public class StackOverflow {

    public func Test() {
        let maybeCFURLs = LSCopyApplicationURLsForBundleIdentifier("com.apple.SFSymbols" as CFString, nil)?.takeRetainedValue()

        let cfURLs = maybeCFURLs as? Array<URL>

        let fontFile = cfURLs!.compactMap { url -> URL? in
            guard let appBundle = Bundle(url: url) else { return nil }
            return appBundle.url(forResource: "SFSymbolsFallback", withExtension: "ttf")
        }

        let attributes = [
            kCTFontTraitsAttribute: [
                kCTFontWeightTrait: NSFont.Weight.regular.rawValue
            ]
        ]

        let provider = CGDataProvider(url: fontFile.first! as CFURL)
        let cgFont = CGFont(provider!)

        let ctAttributes = CTFontDescriptorCreateWithAttributes(attributes as CFDictionary)
        let font = CTFontCreateWithGraphicsFont(cgFont!, 32, nil, ctAttributes)

        // let name = "uni.plus.square.fill.medium" as CFString - Does not work
        //var name = "square.fill.medium" as CFString - Does not work
        let glyphName = "plus.square.fill.medium" as CFString

        var glyph = CTFontGetGlyphWithName(font, glyphName )
    }
}
4

0 回答 0