1

I've got a label on a prototype cell in a table view. I've got some random text in there that I'd like to see change based on the size of the system font. Right now, if I change the system font size, this label stays the same size.

This is mostly for iOS 8. If this works with iOS 7, it'd be even better.

Pascal
  • 16,846
  • 4
  • 60
  • 69
Chris Williams
  • 11,647
  • 15
  • 60
  • 97

4 Answers4

1

In iOS 7 and later, you can use dynamic type:

cell.textLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];

See WWDC 2014 video, What's New in Table and Collection Views for an illustration of how you would do this.

The question is how to change the cell height. In iOS 8, the default tableview cells will change size automatically. If you are using your own prototype cell with custom layout, if you use auto layout and have fully defined constraints for the content view, then then table view height will change automatically for you.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
0

You could use something like this if I got what you mean.

if UIScreen.mainScreen().bounds.size.height == 480 {
    // iPhone 4

label.font = label.font.fontWithSize(20)     
} else if UIScreen.mainScreen().bounds.size.height == 568 {
    // IPhone 5

label.font = label.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.width == 375 {
    // iPhone 6

label.font = label.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.width == 414 {
    // iPhone 6+

label.font = label.font.fontWithSize(20)
} else if UIScreen.mainScreen().bounds.size.width == 768 {
    // iPad
}
Cesare
  • 9,139
  • 16
  • 78
  • 130
  • I think the intent was to link the font size to settings, not to the size of the screen. – Rob Jan 14 '15 at 19:40
0

In the WWDC 2014, Apple introduced a new feature called Self Sizing Cells for the UITableView and the UICollectionView. You could add this to your -(void)viewDidLoad:

tableView.estimatedRowHeight = 44.0; //Or anything else
tableView.rowHeight = UITableViewAutomaticDimension;

Helpful article: Understanding Self Sizing Cells and Dynamic Type in iOS 8

Best regards,

Gabriel Tomitsuka
  • 1,121
  • 10
  • 24
0

Not sure if you are using a specified font. If so, here's the method I use :

+ (int) preferredFontSize : (float) fontSize
{
    NSString *category = [[UIApplication sharedApplication] preferredContentSizeCategory];

    if ([category isEqualToString:UIContentSizeCategoryExtraExtraExtraLarge])
    {
        return fontSize / 0.7;
    }
    else
    {
        if ([category isEqualToString:UIContentSizeCategoryExtraExtraLarge])
        {
            return fontSize / 0.8;
        }
        else
        {
            if ([category isEqualToString:UIContentSizeCategoryExtraLarge])
            {
                return fontSize / 0.9;
            }
            else
            {
                if ([category isEqualToString:UIContentSizeCategoryMedium])
                {
                    return fontSize * 0.9;
                }
                else
                {
                    if ([category isEqualToString:UIContentSizeCategorySmall])
                    {
                        return fontSize * 0.8;
                    }
                    else
                    {
                        if ([category isEqualToString:UIContentSizeCategoryExtraSmall])
                        {
                            return fontSize * 0.7;
                        }
                        else
                        {
                            return fontSize;
                        }
                    }
                }
            }
        }
    }
}

@end