Michael
Michael

Reputation: 6513

How do I create a bold UIFont from a regular UIFont?

If I have a UIFont object, is it possible to convert it to bold? I don't know the font name, I just have a UIFont object. What I want is a function like

UIFont *boldFontFromFont(UIFont *input)
{
    return [input derivedFontWithFontWeight:UIFontWeightBold];
}

How can I change the code so that it works. (The code above does not work, I just made it up to illustrate the point.)

Thanks in advance.

Upvotes: 43

Views: 27628

Answers (8)

ivanzoid
ivanzoid

Reputation: 6072

Nobody posted a solution which:

  • is for Swift
  • is an extension for UIFont
  • doesn't do force unwraps
  • uses the same point size as source font
  • and only does what is asked in the question (create bold UIFont based on existing UIFont)

so I'm doing it:

import UIKit

extension UIFont {
    func boldFont() -> UIFont? {
        guard let boldDescriptor = fontDescriptor.withSymbolicTraits(.traitBold) else {
            return nil
        }

        return UIFont(descriptor: boldDescriptor, size: pointSize)
    }
}

Feel free to copy-and-paste!

Upvotes: 3

marcprux
marcprux

Reputation: 10385

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.

Upvotes: 102

Joachim Deelen
Joachim Deelen

Reputation: 1131

This is a very old thread but someone may be interested in how to do this in Swift 5 nowadays.

Easy like this:

var font: UIFont = UIFont.systemFont(ofSize: 18)
if let newDescriptor = font.fontDescriptor.withSymbolicTraits(.traitBold) {
    font = UIFont(descriptor: newDescriptor, size: font.pointSize)
}

Upvotes: 9

Nikita Kukushkin
Nikita Kukushkin

Reputation: 15168

Since this question pops up when you search for bold UIFonts in Swift, here's a fresh answer:

extension UIFont {
    /// Returns a new font in the same family with the given symbolic traits,
    /// or `nil` if none found in the system.
    func withSymbolicTraits(_ traits: UIFontDescriptor.SymbolicTraits) -> UIFont? {
        guard let descriptorWithTraits = fontDescriptor.withSymbolicTraits(traits)
            else { return nil }
        return UIFont(descriptor: descriptorWithTraits, size: 0)
    }
}

Example:

myFont.withSymbolicTraits(.taitBold) // returns the bold version of myFont

Upvotes: 2

David H.
David H.

Reputation: 2862

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!

Upvotes: 31

user436818
user436818

Reputation: 267

You can either use

[UIFont boldSystemFontOfSize:12].

If you are using custom fonts you have to use the name directly

[UIFont fontWithName:@"Helvetica-Bold" size:17.0].

You can look up the possible font names with

[UIFont fontNamesForFamilyName:@"American Typewriter"].

In this Post: https://stackoverflow.com/a/15388946/436818 Ben M has an idea how to get the bold version dynamically. But extend the method to be sure to get the bold version (if it exists) because there are other bold versions like CondensedBold too.

Upvotes: 2

loungerdork
loungerdork

Reputation: 991

Attempting to derive the bold/italic font using font or family names no longer works correctly since iOS 7, due to the cryptic font family name of the system font. Below is a simple extension to derive the bold/italic font using the UIFontDescriptor class.

+(UIFont *) font:(UIFont *)font bold:(BOOL)bold italic:(BOOL)italic
{
    NSUInteger traits = 0;
    if (bold)
    {
        traits |= UIFontDescriptorTraitBold;
    }
    if (italic)
    {
        traits |= UIFontDescriptorTraitItalic;
    }
    return [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:traits] size:font.pointSize];
}

Upvotes: 7

filwag
filwag

Reputation: 690

To get a bold font you need to pass a specific name of the font from a font family. You can get a font family name from a given font, then list all fonts from this family. In general, a bold font will contain "bold" in its name, but the format isn't strict and there could be variations like "Helvetica-BoldOblique", for example. You can start from this code:

- (UIFont *)boldFontFromFont:(UIFont *)font
{
    NSString *familyName = [font familyName];
    NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
    for (NSString *fontName in fontNames)
    {
        if ([fontName rangeOfString:@"bold" options:NSCaseInsensitiveSearch].location != NSNotFound)
        {
            UIFont *boldFont = [UIFont fontWithName:fontName size:font.pointSize];
            return boldFont;
        }
    }
    return nil;
}

Upvotes: 14

Related Questions