首页 > 移动平台 > 详细

iOS Hex 转 UIColor

时间:2015-07-26 05:56:14      阅读:190      评论:0      收藏:0      [点我收藏+]

+ (UIColor *)colorWithHex:(NSString *)string

{

    NSString *cleanString = [string stringByReplacingOccurrencesOfString:@"#" withString:@""];

    if([cleanString length] == 3) {

        cleanString = [NSString stringWithFormat:@"%@%@%@%@%@%@",

                       [cleanString substringWithRange:NSMakeRange(0, 1)],[cleanString substringWithRange:NSMakeRange(0, 1)],

                       [cleanString substringWithRange:NSMakeRange(1, 1)],[cleanString substringWithRange:NSMakeRange(1, 1)],

                       [cleanString substringWithRange:NSMakeRange(2, 1)],[cleanString substringWithRange:NSMakeRange(2, 1)]];

    }

    if([cleanString length] == 6) {

        cleanString = [cleanString stringByAppendingString:@"ff"];

    }

    

    unsigned int baseValue;

    [[NSScanner scannerWithString:cleanString] scanHexInt:&baseValue];

    

    float red = ((baseValue >> 24) & 0xFF)/255.0f;

    float green = ((baseValue >> 16) & 0xFF)/255.0f;

    float blue = ((baseValue >> 8) & 0xFF)/255.0f;

    

    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];

}

iOS Hex 转 UIColor

原文:http://www.cnblogs.com/houtou118/p/4676965.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!