Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel Disappear From Static UITableViewCell with Dynamic Type

I have static UITableView with Basic style cells, default 44 height. There is a label in each cell with Body text style. This way I'm getting Dynamic Type behaviour for free.

It works except if:

  1. Open app
  2. Open table view
  3. Open Settings ang change text size (bigger of smaller)
  4. Open app again, labels vanishing.
  5. Go to another view (back) and open troubled table again — text changed and looks good

I managed to fix this with following hack

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(preferredContentSizeChanged)
                                                 name:UIContentSizeCategoryDidChangeNotification object:nil];
}

- (void)preferredContentSizeChanged
{
    // adjust the layout of the cells
    // for some reason text from labels are disappear
    self.statusBarCell.textLabel.text = @"Status bar";
    self.itemColorCell.textLabel.text = @"Color";

But I have another exactly the same UITableView and this is not working. I tried outlets for cells and labels. I tried reloadData and setNeedsLayout methods.

Here is pictures. Labels are yellow and content view is blue:

Before, cells with contentAfter, cells without labels

like image 914
Boris Y. Avatar asked Oct 12 '14 22:10

Boris Y.


2 Answers

I recently played with static tables and tried to add dynamic type to it. I got the same results as you - one even can see this vanishing label in Apple's own Contacts.app.

Even after changing my table from static to dynamic and implementing a small custom cell containing a UILabel (configured with font as UIFontTextStyleBody in the XIB) the table still didn't seem to update its contents when changing the preferred text size in system prefrences.

However: I accidentally got some results after doing the following: in the subclass for my custom cell I implemented prepareForReuse and re-initialize the label's font to the correct style:

- (void)prepareForReuse {

    self.label.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
}

With the hack above my table cells adapt their fonts dynamically to changes in system preferences.

Hope that helps.

like image 179
mschmidt Avatar answered Oct 21 '22 16:10

mschmidt


I changed table to a dynamic and implemented cellForRowAtIndexPath: method to assign table label in code. This is not helped me.

Next I changed table view cell style from basic to custom. I had to add my own label and set up AutoLayout constraints in Storyboard. This is solved the issue. preferredContentSizeChanged notification is no longed necessary.

In my cellForRowAtIndexPath:

cell.label.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];

I will file an Apple radar later. Looks like we can't use Dynamic Type with static tables.

Good luck and please implement Dynamic Type in your apps. It's worth it.

like image 39
Boris Y. Avatar answered Oct 21 '22 15:10

Boris Y.