Skip to content

Instantly share code, notes, and snippets.

@himaratsu
Last active August 29, 2015 14:04
Show Gist options
  • Save himaratsu/c25440d8c5513837ac4c to your computer and use it in GitHub Desktop.
Save himaratsu/c25440d8c5513837ac4c to your computer and use it in GitHub Desktop.
Xcodeで色を操る

Hex Color Picker

OMColorSense

  • UIColorにカーソルがあたると、ポップアップで色を表示してくれる
  • (ここにスクショ)
  • 導入方法
    • alcatraz を入れる
    • Window > Package Manager
      • OMColorSenseをインスコ

UIColor+Hex(カテゴリ拡張)

  • 書いておくと便利だよ

UIColor+Hex.h

+ (UIColor*)colorWithHexString:(NSString *)hex;
+ (UIColor*)colorWithHexString:(NSString *)hex alpha:(CGFloat)a;

UIColor+Hex.m

+ (UIColor*)colorWithHexString:(NSString *)hex {
    NSScanner *colorScanner = [NSScanner scannerWithString:hex];
    unsigned int color;
    if (![colorScanner scanHexInt:&color]) return nil;
    CGFloat r = ((color & 0xFF0000) >> 16)/255.0f;
    CGFloat g = ((color & 0x00FF00) >> 8) /255.0f;
    CGFloat b =  (color & 0x0000FF) /255.0f;
    return [UIColor colorWithRed:r green:g blue:b alpha:1.0];
}

+ (UIColor*)colorWithHexString:(NSString *)hex alpha:(CGFloat)a {
    NSScanner *colorScanner = [NSScanner scannerWithString:hex];
    unsigned int color;
    if (![colorScanner scanHexInt:&color]) return nil;
    CGFloat r = ((color & 0xFF0000) >> 16)/255.0f;
    CGFloat g = ((color & 0x00FF00) >> 8) /255.0f;
    CGFloat b =  (color & 0x0000FF) /255.0f;
    return [UIColor colorWithRed:r green:g blue:b alpha:a];
}

こんな感じで使う

myLabel.textColor = [UIColor colorWithHexString:@"ebebeb"];
  • よく使うものは#defineもオススメ
#define kDefaultTextColor [UIColor colorWithRed:70/255.0 green:171/255.0 blue:235/255.0 alpha:1.0]

Digital Color Picker(Mac)

*色のコードを抜ける(ちょっと怪しい)

Webサイトの色を抜く

  • Condule みたいな名前のやつ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment