Latest 1.1.4
Homepage https://github.com/nicolasgoutaland/NSString-Color
License MIT
Platforms ios 5.0, watchos 2.0, requires ARC
Authors

NSString+Color Build Status

Category on NSString allowing simple color instantiation from its content. Support web colors and custom colors code also.

Description

This category adds following methods to NSString.

- (UIColor *)colorFromRGBcode   // Consider string as an hexadecimal RGB code
- (UIColor *)colorFromRGBAcode  // Consider string as an hexadecimal RGBA code
- (UIColor *)colorFromName      // Consider string as a webcolor name, UIColor selector name or custom color code
- (UIColor *)representedColor   // This method will automatically choose between three other methods in order to generate a color

+ (UIColor *)webColorForKey:(NSString *)aWebColorName; // Return associated web color

+ (void)registerColor:(UIColor *)aColor withKey:(NSString *)aKey; // Register a custom color for given key
+ (void)registerColors:(NSDictionary *)colors;                    // Register a bunch of custom colors
+ (void)clearRegisteredColorForKey:(NSString *)aKey;              // Remove a previously registered color
+ (UIColor *)registeredColorForKey:(NSString *)aKey;              // Retreive a previously registered color

This allows you to load color codes from configuration file, such as PLIST and create colors to configure your views background color for example.
All created colors are cached using NSCache, allowing you to reuse them.
Webcolor names are case insensitive, and you can specify UIColor selectors name without Color suffix.

For a list of webcolors, you can have a look at Wikipedia Web colors page

ScreenShot

Last update added support for custom color keys, allowing you to define custom code. You can for example define a titleColor key and use it in your application. Custom colors can be retrieved using representedColor, colorFromName and registeredColorForKey methods. This can be really helpful when styling an entire application, allowing you to update color through all screens, just by editing color from a PLIST file for example.

ScreenShot

Usage

  • simply call one of added method on an NSString object
  • Preferred way is using representedColor method which will detect right color format

Example

  [@"red" representedColor];        // UIColor selector name without suffix
  [@"redColor" representedColor];   // UIColor full selector name
  [@"#FFEE55" representedColor];    // RGB Hexadecimal code. Prefix with # to force hexadecimal method
  [@"FFEE55" representedColor];     // RGB Hexadecimal code. Will also work, but will be checked through a regex first
  [@"#eeEEEe77" representedColor];  // RGB and RGBA Hexadecimal code are case insensitive too
  [@"roSyBroWn" representedColor];  // Web color case insensitive

  [@"FFEE55" colorFromRGBcode];     // Hexadecimal code
  [@"red" colorFromName];           // UIColor selector name, without color suffix
  [@"redColor" colorFromName];      // UIColor full selector name
  [@"red" colorFromName];           // UIColor selector name, without color suffix
  [@"roSyBroWn" colorFromName];     // Web color case insensitive

Custom color example

  [NSString registerColor:[@"#eeeeee"] withKey:@"descriptionText"]; /// Register a custom color
  [@"descriptionText" representedColor];  // Retrieve a custom color
  [@"dEscRiptIONText" representedColor];  // Custom color names are case insensitive

Advantages

  • Allows you to load colors from configuration files
  • Remove all boilerplate code induced by format checking
  • Add web colors

Bonus

If you are using UIColor categories to add some color code to your project, you can load them from configuration file too. Be careful, selector name are case sensitive.

  • FPBrandColors

    [@"Facebook" representedColor];                      // UIColor selector name from FPBrandColors
    [@"Github" representedColor];                        // UIColor selector name from FPBrandColors
  • UIColor-FlatColors

    [@"flatEmeraldColor" representedColor];              // UIColor selector name from UIColor-FlatColors
    [@"flatPomegranateColor" representedColor];          // UIColor selector name from UIColor-FlatColors
  • UIColor-CSSColors
    [@"cssBurlywoodColor" representedColor];             // UIColor selector name from UIColor-CSSColors
    [@"cssLightgoldenrodyellowColor" representedColor];  // UIColor selector name from UIColor-CSSColors

Consideration

While colorFromName will return nil if color name is unknown, colorFromRGBAcode and colorFromRGBcode will return you an undefined color if hexadecimal code is invalid.
Same apply to representedColor when forcing hexadecimal format (Prefixing with #)

Installation

Cocoapods: pod 'NSString+Color'
Manual: Copy the Classes folder in your project

Import header in your project. .pch is a good place ;)

#import "NSString+Color.h"

Versions

1.1.4 : Added support for Watch OS 2.0, thanks to PR #1 from pivl
1.1.3 : Added library target
1.1.2 : Updated documentation
1.1.1 : Fixed a bug handling cache in + registeredColorForKey method
1.1 : Added support for custom colors codes
1.0 : Initial release

Latest podspec

{
    "name": "NSString+Color",
    "version": "1.1.4",
    "summary": "UIColor creation from NSString",
    "description": "NSString category allowing you to create UIColor from it.nnThis allows you to create color from hexadecimal representation or color name, such as @"#FF4465", @"red", @"antiquewhite"nAs a bonus, category also embeds a list of predefined web colors, according to http://en.wikipedia.org/wiki/Web_colorsnCategory also works with FPBrandColors, UIColor-FlatColors and UIColor-CSSColors, since these are categories for UIColor.nnMain use case is when loading color configuration from configuration file.nLast version add some method allowing you to register custom colors code, to be used afterward, allowing you to style your application with color codes.nnARC only",
    "homepage": "https://github.com/nicolasgoutaland/NSString-Color",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "nicolasgoutaland": "[email protected]"
    },
    "source": {
        "git": "https://github.com/nicolasgoutaland/NSString-Color.git",
        "tag": "1.1.4"
    },
    "screenshots": "https://raw.github.com/nicolasgoutaland/NSString-Color/master/Assets/NSString+Colors.gif",
    "source_files": [
        "Classes",
        "Classes/**/*.{h,m}"
    ],
    "exclude_files": "Classes/Exclude",
    "requires_arc": true,
    "prefix_header_contents": "#ifndef TARGET_OS_IOSn  #define TARGET_OS_IOS TARGET_OS_IPHONEn#endifn#ifndef TARGET_OS_WATCHn  #define TARGET_OS_WATCH 0n#endif",
    "platforms": {
        "ios": "5.0",
        "watchos": "2.0"
    },
    "ios": {
        "frameworks": "UIKit"
    },
    "watchos": {
        "frameworks": "UIKit"
    }
}

Pin It on Pinterest

Share This