40

How to get the font-size of a UIFont instance?

Or, if someone can implement this method for UIFont?

- (UIFont *)boldFont;
6
  • 26
    You are looking for pointSize of UIFont. Jul 1, 2011 at 6:04
  • To: @Deepak, is pointSize same to fontSize?
    – Míng
    Jul 1, 2011 at 6:09
  • 4
    @iwill: Yes, pointSize will be equal to the value that you provide to the size: parameter of the UIFont constructor. If you read the documentation for UIFont you will see that the fontSize/fontOfSize methods take or return values in points. Jul 1, 2011 at 6:11
  • @Deepak: Are you sure? Why is it named pointSize but not size or fontSize?
    – Míng
    Jul 1, 2011 at 6:15
  • Yes, I am sure. And it's like @Jason has mentioned. Jul 1, 2011 at 6:21

7 Answers 7

39

This is an old answer and it's no longer the best solution, please see the accepted answer instead.

-(UIFont *)boldFont{

//First get the name of the font (unnecessary, but used for clarity)
NSString *fontName = self.fontName;

//Some fonts having -Regular on their names. so we have to remove that before append -Bold / -BoldMT
fontName = [[fontName componentsSeparatedByString:@"-"] firstObject];

//Then append "-Bold" to it.
NSString *boldFontName = [fontName stringByAppendingString:@"-Bold"];

//Then see if it returns a valid font
UIFont *boldFont = [UIFont fontWithName:boldFontName size:self.pointSize];

//If it's valid, return it
if(boldFont) return boldFont;

//Seems like in some cases, you have to append "-BoldMT"
boldFontName = [fontName stringByAppendingString:@"-BoldMT"];
boldFont = [UIFont fontWithName:boldFontName size:self.pointSize];

//Here you can check if it was successful, if it wasn't, then you can throw an exception or something.
return boldFont;

}
3
  • 1
    At the end: if (boldFont) return boldFont; return self;
    – Míng
    Jul 1, 2011 at 9:05
  • 2
    And: NSString *fontName = self.fontName; should be NSString *fontName = self.familyName; // Then it will be perfect, thanks!
    – Míng
    Jul 1, 2011 at 9:22
  • 2
    Appending -BoldMT won't work for a font such as ArialMT; its bold equivalent is Arial-BoldMT, not ArialMT-BoldMT. Jul 4, 2011 at 0:03
27

To access the font size from your UIFont instance use

 @property(nonatomic, readonly) CGFloat pointSize

There are more property to tell the font size (for cap, small etc...)

capHeight,xHeight,pointSize 

Use below to access the bold font

UIFont* myboldFont = [UIFont boldSystemFontOfSize:11];
1
  • 4
    The boldFont: method is completely useless; it's just a wrapper for boldSystemFontOfSize:. It seems obvious that @iwill was asking how to convert an existing UIFont to its bold counterpart. Jul 1, 2011 at 6:04
16

UIFontDescriptor is pretty powerful class and can be used to get bold version of the font you want. It should be totally safe and future proof as it only requires usage of public API and doesn't depend on any string modifications.

Here is the code in Swift 3:

extension UIFont {

    func bold() -> UIFont? {
         let fontDescriptorSymbolicTraits: UIFontDescriptorSymbolicTraits = [fontDescriptor.symbolicTraits, .traitBold]
         let bondFontDescriptor = fontDescriptor.withSymbolicTraits(fontDescriptorSymbolicTraits)
         return bondFontDescriptor.flatMap { UIFont(descriptor: $0, size: pointSize) }
    }

}

Here is the code in objective-c:

@implementation UIFont (RABoldFont)

- (UIFont *)ra_boldFont
{
    UIFontDescriptor *fontDescriptor = [self fontDescriptor];
    UIFontDescriptorSymbolicTraits traits = fontDescriptor.symbolicTraits;
    traits = traits | UIFontDescriptorTraitBold;
    UIFontDescriptor *boldFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:traits];
    return [UIFont fontWithDescriptor:boldFontDescriptor size:self.pointSize];
}

@end
2
  • This is the best answer since iOS 7.
    – Míng
    Mar 9, 2016 at 10:16
  • Is [fontDescriptor.symbolicTraits, .traitBold] equivalent to fontDescriptor.symbolicTraits.union(.traitBold) ? Jul 13, 2018 at 10:49
8

Here is the easiest way to find font size

UIFont *anyFont;
//initialization and other stuff here

Now at some point you want to find its size

 CGFloat fontSize= [anyFont pointSize];
2

Use the NSString+UIKit methods to measure the size a string with a certain font.

The iPhone treats the normal and bold fonts of the same name as completely separate fonts, and as such there is no simple way to convert between them. For instance, ArialMT and Arial-BoldMT are considered completely different fonts by the OS.

EDIT: I may have misunderstood your question. Perhaps you are looking for the pointSize property of UIFont?

0

You can check for bold with Core Text. Use kCTFontBoldTrait.

0

Swift 3 version of rafał-augustyniak answer

extension UIFont {


    func boldFont() -> UIFont? {
        let fontDescriptor = self.fontDescriptor
        let fontDescriptorSymbolicTraits: UIFontDescriptorSymbolicTraits = [ fontDescriptor.symbolicTraits, .traitBold ]

        guard let boldFontDesc = fontDescriptor.withSymbolicTraits(fontDescriptorSymbolicTraits) else {
            return nil
        }

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

}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.