iosuitableviewxibawakefromnib

Font and textColor are gone - Yet another UITableViewCell awakeFromNib


I don't know if this is specific to UITableViewCells but rather general for UIViews (as i believe) but i noticed the problem with a cell.

As mentioned, I have a custom UITableViewCell subclass which loads itself from a xib when it gets initialized with an designated init:

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:nil options:nil];
    self = nib[0];
    if (self) {
        // Initialization code
        _reuseIdentifier = reuseIdentifier;
    }
    return self;
}

It has a UILabel as an outlet and I set some properties of it in the awakeFromNib method:

- (void)awakeFromNib{
    [super awakeFromNib];

    self.labelLeft.textColor = [UIColor grayColor];
    self.labelLeft.font = [UIFont boldSystemFontOfSize:15.0f];
}

The point is, that the label doesn't keep the textColor nor the font and I don't understand why.
awakeFromNib gets called and the outlet is connected right, since i can set the text.

I can make it work by settings those properties after I set his text in the UITableViewDataSource but I don't feel it's the right way and I want to understand why this doesn't work.

Question:
Why doesn't it keep the Font and Textcolor and what can I do to make it work the right way?


Solution

  • I have the same issue with all my labels loaded from .Nib's. Then I found that the problem was in UIAppearance. I have the following line in my AppDelegate:

    + (void)styleApplication {
        [[UILabel appearance] setFont:[MGStylesheet defaultLightFontOfSize:17]];
    }
    

    This method update all fonts in my application, and all works fine, until I start loading my UIView's from .Nib. When I removed this, everything start working as expected.