ClassyKit / Classy

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

Changing styleClass dynamically to change styling #40

Closed mattsson closed 10 years ago

mattsson commented 10 years ago

Would it be possible to change the styling of a UIView by changing the styleClass to a different string?

cloudkite commented 10 years ago

You should be able to already. Is it not working for you?

mattsson commented 10 years ago

It works sporadically in this instance. I have a bunch of subclassed UIButtons with UILabels and when a button is selected I change the UILabels' styleClass property.

I have this in my style sheet:

UILabel.red {
    text-color: red;
}

UILabel.blue {
    text-color: blue;
}

And this in my subclassed UIButton:

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];

    if (selected) {
        self.dayLabel.cas_styleClass = @"blue";
    }
    else {
        self.dayLabel.cas_styleClass = @"red";
    }
}

Selecting different buttons look like this:

ishowu-capture

As you can see, the label doesn't always change color with the first tap. If I instead directly set styling then everything works just fine, e.g. like this:

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];

    if (selected) {
        self.dayLabel.textColor = [UIColor blueColor];
    }
    else {
        self.dayLabe.textColor = [UIColor redColor];
    }
}

Any suggestions would be greatly appreciated!

cloudkite commented 10 years ago

@mattsson it could be a problem with how I am coalescing calls to cas_styleClass.

If you remove this line UIView+Additions.m #44 does that fix it?

Also when you select another button how are you reseting the styling on previously selected button?

mattsson commented 10 years ago

@cloudkite Unfortunately that did not fix it.

When I tap a button I iterate through an array of all the buttons and set selected to either YES or NO depending on which button was just tapped:

[self.drawnDates enumerateObjectsUsingBlock:^(TDXDatePickerButton *dayButton, NSUInteger idx, BOOL *stop) {
    dayButton.selected = dayIndex == idx;
}];

When I breakpoint in setSelected: this looks completely as expected, 8 calls with NO and one with YES for the selected button. This is also apparent by the circle correctly switching places, which is set using self.circleImage.hidden = !selected in setSelected:.

cloudkite commented 10 years ago

@mattsson sorry for the late reply. Just committed a few fixes to the scheduling of style updates see https://github.com/cloudkite/Classy/commit/7994d9058d308502614c107f561004c7133717ef. Let me know if it helps.

If it doesn't fix your issue could you provide small sample project that replicates the issue so I can look into it. thanks

mattsson commented 10 years ago

@cloudkite That does fix the issue! Thank you.

cloudkite commented 10 years ago

@mattsson had to make some small tweaks to the same code again. Let me know if its still working fine. thanks!

mattsson commented 10 years ago

@cloudkite It still works fine with your latest commit (11984d11a39b184d98b9d4d21adc2d441d374ce2).