views:

2505

answers:

2

hi all, i am creating a application, in which i pick the color of label from XML file.XML file return the color in Hex format(for ex. #00FF00). so how can i change this value to UIColor

+8  A: 

There is no builtin conversion from a hexadecimal string to a UIColor (or CGColor) that I'm aware of. However, you can easily write a couple of functions for this purpose - for example, see http://arstechnica.com/apple/guides/2009/02/iphone-development-accessing-uicolor-components.ars.

Adam Wright
+1 If you scroll way down, the method in question is +colorWithHexString:.
Rob Napier
+1  A: 

I've found the simplest way to do this is with a macro. Just include it in your header and it's available throughout your project.

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

http://cocoamatic.blogspot.com/2010/07/uicolor-macro-with-hex-values.html

Tom