开发者

how can i place hexadecimal code for color in iphone

开发者 https://www.devze.com 2023-01-16 09:03 出处:网络
hi i am new to iph开发者_开发百科one. I am using the fallowing code for imageview background color

hi i am new to iph开发者_开发百科one. I am using the fallowing code for imageview background color

imageView.backgroundColor = [UIColor redColor];

but i need to set background color using hexadecimal code (e.g. 0xffff) how can I do this?

thank u in advance


Maybe create a category?

@interface UIColor (HexColor)
- (UIColor *)colorWithHex:(unsigned int)hex;
@end

@implementation UIColor (HexColor)
- (UIColor *)colorWithHex:(unsigned int)hex
{
   unsigned int redHex = (hex >> 16) & 0xFF;
   unsigned int greenHex = (hex >> 8) & 0xFF;
   unsigned int blueHex = hex & 0xFF;

   CGFloat redValue = (CGFloat)redHex / (CGFloat)0xFF;
   CGFloat greenValue = (CGFloat)greenHex / (CGFloat)0xFF;
   CGFloat blueValue = (CGFloat)blueHex / (CGFloat)0xFF;

   return [UIColor colorWithRed:redValue green:greenValue blue:blueValue];
}
@end

Notice that I'm using 2 hex values for each color. 0xAABBCC would be AA for red, BB for green and CC for blue.

This would give you a UIColor that you can then use for the background color.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号