如何得到 UILabel 的字体大小和字体名称?

我有一个 UILabel,我设置了一个字体大小和字体名称与 Interface Builder。现在我必须读取 ViewController 中两者的值。

我怎么能这么做?

94473 次浏览

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

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;

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.

Swift:

var currentFontSize = button.titleLabel?.font.pointSize