0
votes

In the past, if I wanted to use bold and italic on the same text, I'd select a font by name, like this:

font = [UIFont fontWithName:@"HelveticaNeue-BoldItalic" size:size];

Now I'm trying to do this with the current iOS system font, San Francisco. This answer says it's not safe to refer to this font by name, and indeed, when I try it, some font-related methods do nothing and some crash the app.

Of course there is a boldSystemFontOfSize and italicSystemFontOfSize method, but no boldItalicSystemFontOfSize method.

Does that leave any other way to use bold+italic with the San Francisco font?

1
@rmaddy That worked! I just wanted a UIFont object, but that answer included how to get that.arlomedia

1 Answers

0
votes

Here's a category based on the answer suggested by @rmaddy:

@interface UIFont (UIFontBoldItalic)
+ (UIFont *)boldItalicSystemFontOfSize:(CGFloat)fontSize;
@end

@implementation UIFont (UIFontBoldItalic)
+ (UIFont *)boldItalicSystemFontOfSize:(CGFloat)fontSize {
    UIFontDescriptor *fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
    uint32_t existingTraitsWithNewTrait = [fontDescriptor symbolicTraits] | UIFontDescriptorTraitBold | UIFontDescriptorTraitItalic;
    UIFontDescriptor *updatedFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:existingTraitsWithNewTrait];
    UIFont *font = [UIFont fontWithDescriptor:updatedFontDescriptor size:fontSize];
    return font;
}
@end

Although this doesn't specify "system font" or "San Francisco" anywhere, I assume the UIFontTextStyleBody will return the system font.