Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Or, if someone can implement this method for UIFont?

- (UIFont *)boldFont; 
like image 258
Mr. Míng Avatar asked Jul 01 '11 05:07

Mr. Míng


2 Answers

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;  } 
like image 62
EmilioPelaez Avatar answered Sep 20 '22 17:09

EmilioPelaez


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]; 
like image 40
Jhaliya - Praveen Sharma Avatar answered Sep 21 '22 17:09

Jhaliya - Praveen Sharma