-2

I want to set hex color code for a button border. how can I do that.help me with this.thanx. I have set color like this.

self.buttonone.layer.borderColor = [UIColor greenColor].CGColor;
Marlon Brando aka Ben
  • 863
  • 1
  • 14
  • 33
  • possible duplicate of [how to set hex color code for background](http://stackoverflow.com/questions/6207329/how-to-set-hex-color-code-for-background) – Blind Ninja Sep 18 '15 at 08:18
  • @anuk you have to accept one solution which is used and helped you so if other user have same problem than he/she found solution easily. – JAY RAPARKA Oct 07 '15 at 06:48

2 Answers2

0

HEX to RGB

// takes @"#123456"
- (UIColor *)colorWithHexString:(NSString *)str 
{
    const char *cStr = [str cStringUsingEncoding:NSASCIIStringEncoding];
    long x = strtol(cStr+1, NULL, 16);
    return [UIColor colorWithHex:x];
}

// takes 0x123456
- (UIColor *)colorWithHex:(UInt32)col 
{
    unsigned char r, g, b;
    b = col & 0xFF;
    g = (col >> 8) & 0xFF;
    r = (col >> 16) & 0xFF;
    return [UIColor colorWithRed:(float)r/255.0f green:(float)g/255.0f blue:(float)b/255.0f alpha:1];
}

RGB to HEX

- (NSString *)hexStringForColor:(UIColor *)color 
{
  const CGFloat *components = CGColorGetComponents(color.CGColor);
  CGFloat r = components[0];
  CGFloat g = components[1];
  CGFloat b = components[2];
  NSString *hexString=[NSString stringWithFormat:@"%02X%02X%02X", (int)(r * 255), (int)(g * 255), (int)(b * 255)];
  return hexString;
}
Blind Ninja
  • 1,063
  • 13
  • 28
0

put below code and you will succeeded in your work.

 self.buttonone.layer.borderColor = [[self colorFromGexString:@"AABBFF"]CGColor];

just put below method and call

- (UIColor *)colorFromHexString:(NSString *)hexString
{
    NSString *cString = [[hexString 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 hasPrefix:@"#"]) cString = [cString substringFromIndex:1];

    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];
}
JAY RAPARKA
  • 1,353
  • 2
  • 13
  • 33