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

前端 未结 4 1557
予麋鹿
予麋鹿 2020-12-23 08:53

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?

4条回答
  •  没有蜡笔的小新
    2020-12-23 09:27

    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;
    

提交回复
热议问题