Unable to use custom fonts in Mac Catalyst app

I'm unable to get custom fonts to work in my Mac Catalyst app.


In iOS, you specify the font filenames with the UIAppFonts array key in Info.plist.


On Mac, you need to specify the ATSApplicationFontsPath key in Info.plist. This is the path relative to Resources that all custom fonts live in.


However in Catalyst, neither of these solutions appear to work.

Replies

My use case for this was an font that only contained symbols/glyphs, and then I'd use them NSAttributedString.


My workaround was to replace these with images in my asset catalog, then use NSTextAttachment to embed that image.


        let attachment: NSTextAttachment
        
        if #available(iOS 13, *) {
            attachment = NSTextAttachment(image: image.withRenderingMode(.alwaysTemplate))
        }
        else {
            attachment = NSTextAttachment()
            attachment.image = image.withRenderingMode(.alwaysTemplate)
        }
            
        let imageString = NSMutableAttributedString(attachment: attachment)
        imageString.addAttribute(.foregroundColor, value: color, range: NSRange(location: 0, length: imageString.length))

NSTextAttachment isn't available on watchOS.


To set the colour of the image attachment, use addAttribute afterwards .foregroundColor.

My workaround it to check if the font is available and load it manually if required:

    if ( ! [UIFont fontWithName:@"GO" size:24] ) { // probably maccatalyst bug, load GO-Regular manually
        NSString *filePath = [bundle pathForResource:@"GO-Regular" ofType:@"ttf"];
        NSURL *fonturl = [NSURL fileURLWithPath:filePath];
        CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((__bridge CFURLRef)fonturl);
        CGFontRef newFont = CGFontCreateWithDataProvider(fontDataProvider);
        CGDataProviderRelease(fontDataProvider);
        CFErrorRef fonterror;
        BOOL didRegisterGORegular = CTFontManagerRegisterGraphicsFont(newFont, &fonterror);
        if ( ! didRegisterGORegular ) {
            NSLog(@"FONTERR=%@",fonterror   );
        }
        CGFontRelease(newFont);
    } // ifend custom install GO-Regular font