Grymjack
Grymjack

Reputation: 539

iOS - Trying to retrieve what the current font size of a label is set to

I'm trying to find a way to programmatically retrieve the current font size of a label. Something like this:

int sizeFont = myLabel.font.labelFontSize;

I know about how to retrieve the point size:

int sizePoint = myLabel.font.pointSize;

Looking for the font size. Thanks for any help!

Here is where the label is initialized called from -(void)viewDidLoad a lot of other code is stripped out for readability

-(void)initializeUIElements
{
    // create a new UIView
    UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];

    // initializing close scroll label
    close_scroll = [[UILabel alloc] initWithFrame:CGRectMake(258, 155, 70, 30)];
    close_scroll.text = @"Close Scroll";
    close_scroll.textColor = [UIColor colorWithRed:(255/255.0) green:(240/255.0) blue:(5/255.0) alpha:1];
    close_scroll.font = [UIFont fontWithName:user.display_font size:10];
    close_scroll.transform = CGAffineTransformMakeRotation(3.14159265358979323846264338327950288 * 1.5);
    [newView addSubview:close_scroll];

    // add the new view as a subview to the superview
    [self.view addSubview:newView];
}

** this is called to update the font for the label. The idea being that the user could come back from a profile configuration screen with a different font type selected. The font is selected fine, just the size gets screwy.

-(void)viewWillAppear:(BOOL)animated
{
    CGFloat test2 = close_scroll.font.pointSize;

    // updating fonts displayed in case of a profile change
    close_scroll.font = [UIFont fontWithName:user.display_font size:test2];
}

Upvotes: 0

Views: 1476

Answers (1)

Grymjack
Grymjack

Reputation: 539

ok, I figured it out. Thanks to everyone who helped. Evidentially, the custom font name I had coming into the routine was bad. So the display defaulted to some system default size without throwing any errors. The below line will work to retrieve a label's current font size.

// if you want to retrieve the font size as a separate value, this will work
int sizeLabelFont = labelOpenScroll.font.pointSize;

// this is actually how I am using the line
labelOpenScroll.font = [UIFont fontWithName:user.display_font size:labelOpenScroll.font.pointSize];

Upvotes: 3

Related Questions