Can colors be reused in Interface Builder?

I have several yellow buttons created using Inteface Builder. All have the same color. I am currently declaring a color in every xib. Can I declare it globally and reuse in all xibs?

+6
source share
6 answers

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

You can use the system color palette to preserve the color, but you still need to apply it to all the buttons every time you decide to change it. Or you can simply use the recently used colors in the color selection, but none of them are dynamic enough.

+7
source

Yes you can do it.

At the bottom of the color picker in the Builder interface, there are a number of squares that you can use to store colors for later use. Drag a color into it from the rectangle where the current color is displayed at the top of the color set to save it, and then simply click the saved color later to use it.

diagram showing how to save colors

+3
source

I do not believe that there is a way to do this completely in the interface builder, unfortunately. However, you can come close with a bit of code. The best way I was able to change the colors of the entire application in one go is to subclass the object you want to colorize (e.g. UILabel) to set the color on 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 the custom class in IB:

Now that you want to change the color of all your UILabels, you can do this by changing one def color And you don’t need to clutter up your code with a lot of appearance options.

+1
source

Definitely definitely!

Create a singleton object (you can name it OksanaColor to be cool) ...

... or, if you're really lazy, the UIColor property is UIColor only, which you can access from your application delegate.

-1
source

You can also add a category to UIColor, so you can use it the same way as when using UIColor. For example, in my application, I add a new file called ApplicationColors, which contains all the colors of my application.

 @interface UIColor (ApplicationColours) +(UIColor *)savaColor; @end 

Implementation:

 @implementation UIColor (ApplicationColours) +(UIColor *)savaColor { return [UIColor colorWithRed:228.0f/255.0f green:86.0f/255.0f blue:86.0f/255.0f alpha:1.0f]; } @end 

Then, to use it in my application, I import ApplicationColours.h and use the same as any other UIColor. i.e:

 label.textColor = [UIColor savaColor]; 
-1
source

Here is a very simple implementation of the category of named colors for UIColor . With this code, in your project, UIColor will remember any colors that you want to keep, and allow you to access your own colors or the colors of the system using +colorWithName:

 @interface UIColor (namedColors) + (UIColor *) colorWithName:(NSString *) name; + (void) setColor:(UIColor *) color forName:(NSString *) name; @end static NSMutableDictionary *colorStorage; @implementation UIColor (namedColors) + (NSMutableDictionary *) colorStorage { if (!colorStorage) colorStorage = [[NSMutableDictionary alloc] initWithCapacity:10]; return colorStorage; } + (UIColor *) colorWithName:(NSString *) name { UIColor *result =[[self colorStorage] valueForKey:name]; // See if we have a color with this name in the colorStorage. if (result) return result; SEL selector = NSSelectorFromString(name); // look for a class method whose selector matches the given name, such as "blueColor" or "clearColor". if ([self respondsToSelector:selector] && (result = [self performSelector:selector])) if ([result isKindOfClass:[self class]]) return result; return nil; } + (void) setColor:(UIColor *) color forName:(NSString *) name { [[self colorStorage] setValue:color forKey:name]; } @end 
-2
source

Source: https://habr.com/ru/post/905686/


All Articles