当前位置

网站首页> 程序设计 > 开源项目 > 程序开发 > 浏览文章

ios UIcolor扩展

作者:小梦 来源: 网络 时间: 2024-05-31 阅读:
+(UIColor *) colorWithHexString: (NSString *) hexString {        NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];    CGFloat alpha, red, blue, green;    switch ([colorString length]) {        case 3: // #RGBalpha = 1.0f;red   = [self colorComponentFrom: colorString start: 0 length: 1 Case:1];green = [self colorComponentFrom: colorString start: 1 length: 1 Case:2];blue  = [self colorComponentFrom: colorString start: 2 length: 1 Case:3];break;        case 4: // #ARGBalpha = [self colorComponentFrom: colorString start: 0 length: 1 Case:0];red   = [self colorComponentFrom: colorString start: 1 length: 1 Case:1];green = [self colorComponentFrom: colorString start: 2 length: 1 Case:2];blue  = [self colorComponentFrom: colorString start: 3 length: 1 Case:3];break;        case 6: // #RRGGBBalpha = 1.0f;red   = [self colorComponentFrom: colorString start: 0 length: 2 Case:1];green = [self colorComponentFrom: colorString start: 2 length: 2 Case:2];blue  = [self colorComponentFrom: colorString start: 4 length: 2 Case:3];break;        case 8: // #AARRGGBBalpha = [self colorComponentFrom: colorString start: 0 length: 2 Case:0];red   = [self colorComponentFrom: colorString start: 2 length: 2 Case:1];green = [self colorComponentFrom: colorString start: 4 length: 2 Case:2];blue  = [self colorComponentFrom: colorString start: 6 length: 2 Case:3];break;        default:return nil;break;    }    return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];}+(NSString*)stringWithColor:(UIColor *)color{    if (color==nil) {        return @"";    }        CGFloat r,g,b,a;    [color getRed:&r green:&g blue:&b alpha:&a];        //rgba    return [NSString stringWithFormat:@"[%d,%d,%d,%f]",(int)(r*255),(int)(g*255),(int)(b*255),a];}+(CGFloat) colorComponentFrom: (NSString *) string start: (NSUInteger) start length: (NSUInteger) length Case:(int) ARGB{    NSString *substring = [string substringWithRange: NSMakeRange(start, length)];    NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];    unsigned hexComponent;    [[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];    switch (ARGB) {        case 0://alphareturn hexComponent / 255.0;break;        case 1://redreturn ( hexComponent )/ 255.0;break;        case 2://greenreturn (hexComponent)/ 255.0;break;        case 3://bluereturn (hexComponent) / 255.0;break;        default:break;    }    return 0;}

调用:[UIColor colorWithHexString:@"#0dc3ce"];

热点阅读

网友最爱