美文网首页小方法
十六进制颜色码转UIColor

十六进制颜色码转UIColor

作者: 灬巴山夜雨 | 来源:发表于2018-05-11 16:56 被阅读0次
/**
 * 十六进制颜色数值转UIColor
 */
+ (UIColor *)colorWithRGBHex:(UInt32)hex {
    int r = (hex >> 16) & 0xFF;
    int g = (hex >> 8) & 0xFF;
    int b = (hex) & 0xFF;
    
    return [UIColor colorWithRed:r/255.0f
                           green:g/255.0f
                            blue:b/255.0f
                           alpha:1.0f];
}

/**
 * 十六进制颜色字符串转UIColor
 */
+ (UIColor *)colorWithHexString:(NSString *)string {
    string = [string stringByReplacingOccurrencesOfString:@"#" withString:@""];
    NSScanner *scanner = [[NSScanner alloc] initWithString:string];
    UInt32 hex;
    if (![scanner scanHexInt:&hex]) { return nil; }
    return [self colorWithRGBHex:hex];
}

相关文章

网友评论

    本文标题:十六进制颜色码转UIColor

    本文链接:https://www.haomeiwen.com/subject/rvzfdftx.html