ios 如何获取 UILabel 的字体大小和字体名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4866138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the font size and font name of a UILabel?
提问by Tim
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.
我有一个 UILabel,我使用 Interface Builder 设置了字体大小和字体名称。现在我必须在我的 ViewController 中读取两者的值。
How can I do this?
我怎样才能做到这一点?
回答by Ned
Add a property to your view controller's .h file:
在视图控制器的 .h 文件中添加一个属性:
@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
将标签链接到 Interface Builder 中“File's Owner”出口下的这个 IBOutlet。如果不使用 ARC,请确保在 -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;
回答by Esqarrouth
Swift:
迅速:
var currentFontSize = button.titleLabel?.font.pointSize
回答by ctd
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.
Pointsize 值不是 UIFont size 属性中使用的字体大小。假设您将界面构建器字体大小设置为 14 并打印 pointSize,您将只得到 11。
回答by Guy Ephraim
you have to attach it to a UILabel IBOutlet, and then, label.font...
你必须把它附加到一个 UILabel IBOutlet,然后,label.font...