How do I create a bold UIFont from a regular UIFont?
And if you are looking for the swift implementation:
let normalFont = UIFont(name: "FONT_NAME", size: CGFloat(20))!
let boldFont = UIFont(descriptor: normalFont.fontDescriptor.withSymbolicTraits(.traitBold)!, size: normalFont.pointSize)
Hope this helps! Cheers!
iOS 7 introduces a new UIFontDescriptor class, which makes it a lot easier:
UIFont *font = [UIFont fontWithName:@"Helvetica Neue" size:12];
NSLog(@"plain font: %@", font.fontName); // “HelveticaNeue”
UIFont *boldFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold] size:font.pointSize];
NSLog(@"bold version: %@", boldFont.fontName); // “HelveticaNeue-Bold”
UIFont *italicFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic] size:font.pointSize];
NSLog(@"italic version: %@", italicFont.fontName); // “HelveticaNeue-Italic”
UIFont *boldItalicFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold | UIFontDescriptorTraitItalic] size:font.pointSize];
NSLog(@"bold & italic version: %@", boldItalicFont.fontName); // “HelveticaNeue-BoldItalic”
For people who got here looking for a Cocoa (macOS) equivalent, UIFontDescriptor
comes from NSFontDescriptor
, available since 10.3.