Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I reuse colors in Interface Builder?

I've got several yellow buttons created using Inteface Builder. All have the same color. Currently I declare color in each xib. Can I declared it globally and reuse across all xibs?

like image 887
Oksana Avatar asked Jan 11 '12 06:01

Oksana


3 Answers

Not possible in Interface Builder. Do it in code, for example by creating special subclass of the button.

You could use system Color Palette to save the color, but you still need to apply it to all buttons every time you decide to change it. Or you can just use Recently Used Colors in the color chooser, but neither way is enough dynamic.

like image 147
Tricertops Avatar answered Nov 12 '22 14:11

Tricertops


Yes, you can do this.

At the bottom of the color picker popup in Interface Builder, there's a row of squares you can use to store colors for later use. Drag a color into it from the rectangle where the current color is shown at the top of the color picker to store it, and then just click a stored color later to use it.

diagram showing how to save colors

like image 33
Mark Amery Avatar answered Nov 12 '22 14:11

Mark Amery


I don't believe there is a way to do this entirely in interface builder, unfortunately. However, you can come pretty close with a little bit of code. The best way I've found to be able to change colors throughout the app in one go is to subclass the item that you want to color (UILabel, for instance) to set the color upon initialization:

@interface HuedUILabel : UILabel
@end

@implementation HuedUILabel

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Initialization code
        self.textColor = [AppConfig primaryColor];
    }
    return self;
}

@end

Then, set the label to have a custom class in IB:

Now, when you want to change the color of all your UILabels, you can do it by changing your one color def AND you don't have to clutter your code with a bunch of appearance assignments.

like image 1
Alex Medearis Avatar answered Nov 12 '22 15:11

Alex Medearis