How can I get the font size and font name of a UILabel?

Ios

Ios Problem Overview


I have a UILabel which I set a font size and a font name with Interface Builder. Now I have to read the values of both in my ViewController.

How can I do this?

Ios Solutions


Solution 1 - Ios

Add a property to your view controller's .h file:

@property (nonatomic, retain) IBOutlet UILabel *label;

Link the label to this IBOutlet under "File's Owner" outlets in Interface Builder. If not using ARC, make sure you release it in -dealloc

- (void)dealloc
{
    [self.label release];
    [super dealloc];
}

Then to get the font name and size all you need is

NSString *fontName = self.label.font.fontName;
CGFloat fontSize = self.label.font.pointSize;

Solution 2 - Ios

Swift:

var currentFontSize = button.titleLabel?.font.pointSize

Solution 3 - Ios

Pointsize value is not the Font Size of used in UIFont size property. Say if you go set interface builder font size to 14 and do a print of the pointSize you'll get only 11.

Solution 4 - Ios

you have to attach it to a UILabel IBOutlet, and then, label.font...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTimView Question on Stackoverflow
Solution 1 - IosNedView Answer on Stackoverflow
Solution 2 - IosEsqarrouthView Answer on Stackoverflow
Solution 3 - IosctdView Answer on Stackoverflow
Solution 4 - IosGuy EphraimView Answer on Stackoverflow