How to get the font-size or a bold-version of UIFont instance

40,141

Solution 1

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

Solution 2

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;

}

Solution 3

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

Solution 4

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

Solution 5

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?

Share:
40,141
Mr. Míng
Author by

Mr. Míng

Swift, Objective-C, Javascript, Keyboardist > _

Updated on December 07, 2020

Comments

  • Mr. Míng
    Mr. Míng over 3 years

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

    Or, if someone can implement this method for UIFont?

    - (UIFont *)boldFont;
    
  • titaniumdecoy
    titaniumdecoy almost 13 years
    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.
  • Mr. Míng
    Mr. Míng almost 13 years
    At the end: if (boldFont) return boldFont; return self;
  • Mr. Míng
    Mr. Míng almost 13 years
    And: NSString *fontName = self.fontName; should be NSString *fontName = self.familyName; // Then it will be perfect, thanks!
  • titaniumdecoy
    titaniumdecoy almost 13 years
    Appending -BoldMT won't work for a font such as ArialMT; its bold equivalent is Arial-BoldMT, not ArialMT-BoldMT.
  • Mr. Míng
    Mr. Míng over 8 years
    This is the best answer since iOS 7.
  • David James
    David James almost 6 years
    Is [fontDescriptor.symbolicTraits, .traitBold] equivalent to fontDescriptor.symbolicTraits.union(.traitBold) ?