Berry Blue
Berry Blue

Reputation: 16482

Make font bold in iOS 13

I'm using the following code to make a font bold in an attributed string. This used to work fine but in iOS 13 it's giving me a bold Times font instead of the system font. I'm aware I can use -boldSystemFontOfSize: but I'm doing it this way to that I can make any font bold. I only see the problem with attributes strings.

UIFont *font = [UIFont systemFontOfSize:18];
UIFontDescriptor *fontDescriptor = [UIFontDescriptor fontDescriptorWithName:font.fontName size:font.pointSize];     
UIFontDescriptor *styleDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:[fontDescriptor symbolicTraits] | UIFontDescriptorTraitBold];
UIFont *boldFont = [UIFont fontWithDescriptor:styleDescriptor size:font.pointSize];

Upvotes: 0

Views: 426

Answers (1)

matt
matt

Reputation: 535231

The problem is

fontDescriptorWithName:font.fontName

In iOS 13 it is forbidden to try to refer to a system font by name.

To go from a font to a font descriptor, just ask for the font’s fontDescriptor.

https://developer.apple.com/documentation/uikit/uifont/1619037-fontdescriptor

Thus the standard pattern is (Swift but I’m sure you can translate)

let f = UIFont(...
let desc = f.fontDescriptor 
let desc2 = desc.withSymbolicTraits(.traitBold) 
let f2 = UIFont(descriptor: desc2!, size: 0)

Upvotes: 3

Related Questions