UIColor (16进制颜色、透明色使用)

来源:互联网 发布:php和java哪个好找工作 编辑:程序博客网 时间:2024/05/01 02:09


RGB与十六进制互转

http://tool.css-js.com/rgba.html


十六进颜色  (#03a9f4 0x050301)

+(UIColor *)becomeHexColor:(NSString *)hexColor   NSString *cString = [[hexColorstringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]]uppercaseString];    if ([cString length] < 6) return [UIColor blackColor];        if ([cString hasPrefix:@"0X"])  cString = [cString substringFromIndex:2];    if ([cString hasPrefix:@"#"])   cString = [cString substringFromIndex:1];    if ([cString length] != 6) return [UIColor blackColor];        NSRange range;    range.location = 0;    range.length = 2;    NSString *rColorValue = [cString substringWithRange:range];    range.location = 2;    NSString *gColorValue = [cString substringWithRange:range];    range.location = 4;    NSString *bColorValue = [cString substringWithRange:range];        unsigned int r, g, b;    [[NSScanner scannerWithString:rColorValue] scanHexInt:&r];    [[NSScanner scannerWithString:gColorValue] scanHexInt:&g];    [[NSScanner scannerWithString:bColorValue] scanHexInt:&b];        UIColor *color = [UIColor colorWithRed:((CGFloat) r / 255.0f) green:((CGFloat) g / 255.0f) blue:((CGFloat) b / 255.0f) alpha:1.0f];    return color;}


0 0