मैंने एक फ़ंक्शन बनाया है जो निम्न मामलों में काम करता है: - 1) # 2 के साथ या बिना) 3 और 6 वर्ण लंबे मूल्य # 000 के साथ-साथ # 000000 3) छह से अधिक अतिरिक्त अंकों के मामले में यह अतिरिक्त अंक
//Function Call
UIColor *organizationColor = [self colorWithHexString:@"#AAAAAAAAAAAAA" alpha:1];
//Function
- (UIColor *)colorWithHexString:(NSString *)str_HEX alpha:(CGFloat)alpha_range{
NSString *noHashString = [str_HEX stringByReplacingOccurrencesOfString:@"#" withString:@""]; // remove the #
int red = 0;
int green = 0;
int blue = 0;
if ([str_HEX length]<=3)
{
sscanf([noHashString UTF8String], "%01X%01X%01X", &red, &green, &blue);
return [UIColor colorWithRed:red/16.0 green:green/16.0 blue:blue/16.0 alpha:alpha_range];
}
else if ([str_HEX length]>7)
{
NSString *mySmallerString = [noHashString substringToIndex:6];
sscanf([mySmallerString UTF8String], "%02X%02X%02X", &red, &green, &blue);
return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha_range];
}
else
{
sscanf([noHashString UTF8String], "%02X%02X%02X", &red, &green, &blue);
return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha_range];
}}
स्रोत
2016-10-03 13:21:05
पर ध्यान नहीं देता डॉक्स, 'के अनुसार - (bool) scanHexInt: (अहस्ताक्षरित *) intValue', ताकि आप' 'unsigned' के रूप में घोषित करने के लिए baseColor' चाहते हो सकता है; इसके अलावा, मुझे प्रोग्रामर का जवाब पसंद है। – Isaac