I let my user choose a font from their installed fonts, then use that font in three different contexts in my macOS app.
- The list of fonts itself uses
NSFontDescriptorto create an attributed string, then displays that in the list of choices:
NSFontDescriptor *fontDescriptor = [NSFontDescriptor fontDescriptorWithName:@"Candara" size:14];
NSDictionary *attributes = @{NSFontAttributeName: [NSFont fontWithDescriptor:fontDescriptor size:14]};
attributedString = [[NSAttributedString alloc] initWithString:@"New Attributed String" attributes:attributes];
- In some places I have a very small HTML document where I set the body font to the selected font, convert to an attributed string, and display in an
NSTextField.
NSString *htmlString = @"<!DOCTYPE html>"
"<html>"
"<head>"
"<style>"
"body { font-family: 'Candara', serif; font-size: 14px; }"
"</style>"
"</head>"
"<body>"
"<p>Text with <b>bold</b> or <i>italics</i> maybe.</p>"
"</body>"
"</html>";
NSData *htmlData = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)};
NSError *error;
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:htmlData
options:options
documentAttributes:nil
error:&error];
- Finally, I have places where I have longer HTML documents with the same font used as the body font and displayed in a
WKWebView.
This was all working well for years. When macOS 14 came along, what I find is that option 1 (initializing an NSAttributedString with some text using the Candara font) works fine, as does option 3 (displaying a lot of HTML that uses the Candara font in a WKWebView). But option 2 doesn't work at all. The output is all the "undefined character" glyph — a box containing a question mark, one for each character in the string.
I'm seeing this behavior when the user selects Calibri or Candara, which are both Microsoft fonts. I'm also seeing it with SF Pro, which is an Apple font. Any other font is fine. (At least from what I've seen so far.)
Providing a fallback font (as shown above with "serif") doesn't change anything. The rendering does not fall back to the fallback font; it seems to really think it can use Candara when obviously it can't.
This behavior very definitely appeared with the introduction of macOS 14, and mostly affects those who have chosen one of the Microsoft Office fonts (Calibri and Candara for sure). But as I said, other fonts can produce these results.
Proxima Novadoesn't work either, at least not on my side.