简体   繁体   中英

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];

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM