我想为按钮边框设置十六进制颜色代码。我怎么能这样做。帮助我.thanx。我设置了这样的颜色。
self.buttonone.layer.borderColor = [UIColor greenColor].CGColor;
答案 0 :(得分:0)
HEX
至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
至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;
}
答案 1 :(得分:0)
将代码放在下面,您将成功完成工作。
self.buttonone.layer.borderColor = [[self colorFromGexString:@"AABBFF"]CGColor];
只需输入以下方法并致电
- (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];
}