Override iOS asset catalog colors in code

326 views Asked by At

I am developing an app with the primary BG color 'green' and 'white' as FG (e.g. for buttons). I'd like the user to select "blue" as primary color (and also pick their very own). My algorithms already compute other colors (e.g. light and dark for buttons and gradients, even shadow colors) from the given base color.

I would like to select/choose colors from the Xcode Interface Builder (through defining them in an asset catalog). User-level overridden colors should not be loaded from the real asset catalog but be retrieved by querying my code.

Now the idea: Can I override some class that will allow me to return a UIColor according to the user's choice? I thought it could work like this:

@implementation MyOverriddenAssets

- (UIColor*)colorForName:(NSString* const)name {
  if ([@"primary_color" isEqualToString:name]) {
    if (self.userPrimaryColor == nil) {
      return [super colorForName:name];
    } else {
      return self.userPrimaryColor;
    }
  }
}

@end

And somewhere else I'd probably have to register my custom catalog:

[assets registerCustomAssetCatalog:[MyOverriddenAssets new]];

I am looking for the class to override and the method name for '-colorForName:'. Searching the Apple docs was not successful, so far.

Anyone an idea, except for Use multiple targets with custom catalogs.? My App is single-target and should offer the option of choosing a custom color for reasons of the user's aesthetics and/or visual impairment.

0

There are 0 answers