2009-10-11 4 views
0

Comment puis-je définir CIColor à partir du format de couleur HTML? Par exemple je lis dans le fichier "# e0e0e0" et j'ai besoin de mettre cette couleur à UILable. quand j'essaie d'utiliser [UIColor colorWithRed: vert: bleu: alpha: 1.0] j'ai trouvé que c'est un nombre float qui définit la couleur en%. Comment puis-je utiliser le format de couleur LONG qui définit backgroundColor à UILable?Couleur WEB vers CIColor

Répondre

2

Chaque couleur est un nombre hexadécimal avec un maximum de 0xff (255 décimal). Divisez la valeur actuelle par 255 et vous aurez votre float pour créer un CIColor.

3

tiré de ce guide: http://arstechnica.com/apple/guides/2009/02/iphone-development-accessing-uicolor-components.ars

// Separate into r, g, b substrings 
NSRange range; 
range.location = 0; 
range.length = 2; 
NSString *rString = [cString substringWithRange:range]; 

range.location = 2; 
NSString *gString = [cString substringWithRange:range]; 

range.location = 4; 
NSString *bString = [cString substringWithRange:range]; 

// Scan values 
unsigned int r, g, b; 
[[NSScanner scannerWithString:rString] scanHexInt:&r]; 
[[NSScanner scannerWithString:gString] scanHexInt:&g]; 
[[NSScanner scannerWithString:bString] scanHexInt:&b]; 

return [UIColor colorWithRed:((float) r/255.0f) 
         green:((float) g/255.0f) 
         blue:((float) b/255.0f) 
         alpha:1.0f]; 
+1

Si vous ne voulez pas les frais généraux de NSScanner, regardez strtol(). – nall

Questions connexes