ClassyKit / Classy

Expressive, flexible, and powerful stylesheets for UIView and friends.
http://classykit.github.io/Classy/
MIT License
740 stars 77 forks source link

Don't allow global fonts to override attributedText on UILabels #29

Closed bguthrie closed 10 years ago

bguthrie commented 10 years ago

I'm running into an issue in which a global style set on a UILabel appears to override more specific styles attached to the attributedText attribute of a particular label––in my case, an icon font and color. Here's roughly what my .cas file looks like:

UILabel {
    font: HelveticaNeue-Thin 17.0;
    text-color: #333;
}

And then, in code:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    FAKIcon *icon = [FAKFontAwesome trophyIconWithSize:20];
    [icon addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor]];
    self.trophyLabel.attributedText = [icon attributedString];
}

My expectation is that the attributedText font properties should always take precedence––indeed, the idea that a global UILabel setting could override this everywhere seems rather alarming. Is this an iOS limitation? Am I nuts?

cloudkite commented 10 years ago

@bguthrie styling takes place in didMoveToWindow which happens after viewWillAppear. Do you get different result if you place your code in viewDidAppear? which happens after didMoveToWindow

cloudkite commented 10 years ago

I had a look into this and it is a timing issue

ie if you have the following code :

cell.textLabel.textColor = [UIColor purpleColor];
cell.textLabel.attributedText = [[NSAttributedString alloc] initWithString:@"hello" attributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] }];

The color will be black as it is set last

However swapping the order :

cell.textLabel.attributedText = [[NSAttributedString alloc] initWithString:@"hello" attributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] }];
cell.textLabel.textColor = [UIColor purpleColor];

The color will be purple as it is set last

So using viewDidAppear instead of viewWillAppear should fix your issue

bguthrie commented 10 years ago

Thanks so much! That's really helpful.

On Thu, Jan 23, 2014 at 9:36 AM, Jonas Budelmann notifications@github.comwrote:

Closed #29 https://github.com/cloudkite/Classy/issues/29.

— Reply to this email directly or view it on GitHubhttps://github.com/cloudkite/Classy/issues/29 .