博客> iOS开发:十六进制颜色转UIColor
iOS开发:十六进制颜色转UIColor
2018-12-16 01:27 评论:0 阅读:502 yao7778899
颜色转换 UIColor web/h5颜色

Objective-C

UIColor * __nullable UIColorFromHexValue(NSUInteger hexValue) {

    CGFloat red = (hexValue & 0xFF0000) >> 16;
    CGFloat green = (hexValue & 0x00FF00) >> 8;
    CGFloat blue = hexValue & 0x0000FF;

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

使用

UIColor *myColor = UIColorFromHexValue(0xFFB6C1);

Swift

extension UIColor {

    static func hex(_ hexValue: UInt) -> UIColor {

        let red = (hexValue & 0xFF0000) >> 16
        let green = (hexValue & 0x00FF00) >> 8
        let blue = hexValue & 0x0000FF

         return UIColor(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: 1.0)
    }
}

使用

let myColor = UIColor.hex(0xFFB6C1)
收藏
0
sina weixin mail 回到顶部