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

Mr. Ming picture Mr. Ming · Jul 1, 2011 · Viewed 39.2k times · Source

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

Or, if someone can implement this method for UIFont?

- (UIFont *)boldFont;

Answer

EmilioPelaez picture EmilioPelaez · Jul 1, 2011

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;

}