How to define and use a central color palette for user interface elements?

I would like to define a color palette in one place and just use the links to it, so I can change the palette in this one place without touching all the interface elements to change the color.

To clarify, here is how I could achieve the same thing in android:

  • define colors.xml:
<resources> <color name="main_text">#ffffffff</color> </resources> 
  • use colors as follows:
 <TextView ... android:text="foobar" android:textColor="@color/main_text" /> 

In the iOS SDK, the user interface is usually not described by manually written xml. But I was wondering if there is a way to use some sort of color reference in IB, and then override the color later. (I don't need to see the final colors in the xcode preview.)

So far, the only solution I have found would be to use type tags: define a tag for each element, get them at runtime, apply to the correct class and start applying color. This is very cumbersome.

+4
source share
1 answer

I would make a category for the UIColor class. Thus, you can declare complementary colors as follows:

 + (UIColor *)lightBlueColor { return [UIColor colorWithRed:132.0f / 255.0f green:207.0f / 255.0f blue:218.0f / 255.0f alpha:1.0f]; } 

And then use them just like any other color:

 [self.view setBackgroundColor:[UIColor lightBlueColor]]; 

Otherwise, you might consider subclassing which user interface element you want to repaint. That way, you can make color changes to initWithCoder: and then refer to a subclass in Interface Builder.

+2
source

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


All Articles