23
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

#RRGGBB(16進RGB)からUIColorを作る

Last updated at Posted at 2013-02-22

初投稿です

htmlなんかでよく使う#RRGGBBの色からUIColorを生成したい場合のメソッドです。
UIColorのカテゴリとして宣言します。

UIColor+Hex.m
@implementation UIColor (Hex)
+ (id)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;
	//NSLog(@"HEX to RGB >> r:%f g:%f b:%f a:%f\n",r,g,b,a);
	return [UIColor colorWithRed:r green:g blue:b alpha:a];
}
@end
hogehoge.m
cell.backgroundColor = [UIColor colorWithHexString:@"97E7FF" alpha:1.0f];

みたいな感じでつかってます

23
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?