1

I am attempting to glyphs from the SF Symbol front using the code sample below provided by an extract https://github.com/davedelong/sfsymbols. I have isolated the code that I can't get to work with values extracted from a run time.

I have condensed the code down to only the statements required to produce the problem.

This issue seems to be the name I am providing to the final statement in CTFontGetGlyphWithName, Every combination of values I have attempted returns 0.

Please note that you have to have the SF Symbols application installed for it to acquire the initial bundle.

Please excuse the code quality, this is just to create the shortest possible example of code to reproduce the problem.

thanks.

I have followed quite a few snipped and tutorials but there doesn't seem to be a great deal of practical examples of these more advanced functions available.

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 )
    }
}

0 Answers0