Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set hex color code for background [duplicate]

Tags:

ios

hex

uicolor

Possible Duplicate:
How can I create a UIColor from a hex string?

I want to programmatically set the color of the UIView Background.

It doesn't seem like I can do it through Interfacebuilder. How should I do it if I want to set it to some hex code color?

like image 749
SuperString Avatar asked Jun 01 '11 20:06

SuperString


People also ask

Can two hex codes be the same color?

Meaning the two hex values are actually the same colour with same saturation.

How do I change the background color in hexadecimal?

Background-color values can be expressed in hexadecimal values such as #FFFFFF, #000000, and #FF0000. Background-color values can be expressed using rgb such as rgb(255,255,255), rgb(0,0,0), and rgb(255,0,0). Background-color values can be expressed as named colors such as white, black, and red.

How do I copy a hexadecimal color?

Just click the little eyedropper in the bottom bar and click anywhere in the browser window. The Hex Code is displayed right in the bottom bar or you can right-click the eyedropper and copy the color out in a variety of formats.


2 Answers

I like to use this little piece of code to use HTML web colors in my apps.

Usage:

[self.view setBackgroundColor: [self colorWithHexString:@"FFFFFF"]]; /* white */ 

The Code:

-(UIColor*)colorWithHexString:(NSString*)hex   {       NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];        // String should be 6 or 8 characters       if ([cString length] < 6) return [UIColor grayColor];        // strip 0X if it appears       if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];        if ([cString length] != 6) return  [UIColor grayColor];        // 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];   }  
like image 192
WrightsCS Avatar answered Sep 22 '22 19:09

WrightsCS


Here's simple category on UIColor that helps you create color from 8bit int value
and from hex value ("#a2ffc0").

UIColor+CreateMethods.h

// //  UIColor+CreateMethods.h // //  Created by Tomasz Rybakiewicz on 1/13/12. //  #import <UIKit/UIKit.h>  @interface UIColor (CreateMethods)  // wrapper for [UIColor colorWithRed:green:blue:alpha:] // values must be in range 0 - 255 + (UIColor*)colorWith8BitRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha;  // Creates color using hex representation // hex - must be in format: #FF00CC  // alpha - must be in range 0.0 - 1.0 + (UIColor*)colorWithHex:(NSString*)hex alpha:(CGFloat)alpha;  @end 

UIColor+CreateMethods.m

// //  UIColor+CreateMethods.m // //  Created by Tomasz Rybakiewicz on 1/13/12. //  #import "UIColor+CreateMethods.h"  @implementation UIColor (CreateMethods)  + (UIColor*)colorWith8BitRed:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha {     return [UIColor colorWithRed:(red/255.0) green:(green/255.0) blue:(blue/255.0) alpha:alpha]; }  + (UIColor*)colorWithHex:(NSString*)hex alpha:(CGFloat)alpha {      assert(7 == [hex length]);     assert('#' == [hex characterAtIndex:0]);      NSString *redHex = [NSString stringWithFormat:@"0x%@", [hex substringWithRange:NSMakeRange(1, 2)]];     NSString *greenHex = [NSString stringWithFormat:@"0x%@", [hex substringWithRange:NSMakeRange(3, 2)]];     NSString *blueHex = [NSString stringWithFormat:@"0x%@", [hex substringWithRange:NSMakeRange(5, 2)]];      unsigned redInt = 0;     NSScanner *rScanner = [NSScanner scannerWithString:redHex];     [rScanner scanHexInt:&redInt];      unsigned greenInt = 0;     NSScanner *gScanner = [NSScanner scannerWithString:greenHex];     [gScanner scanHexInt:&greenInt];      unsigned blueInt = 0;     NSScanner *bScanner = [NSScanner scannerWithString:blueHex];     [bScanner scanHexInt:&blueInt];      return [UIColor colorWith8BitRed:redInt green:greenInt blue:blueInt alpha:alpha]; }  @end 

Enjoy.

like image 45
Tomasz Rybakiewicz Avatar answered Sep 24 '22 19:09

Tomasz Rybakiewicz