美文网首页
iOS开发 UIColor工具

iOS开发 UIColor工具

作者: wh_mouse | 来源:发表于2017-07-07 11:12 被阅读51次

    一、宏定义(10进制表示RGB)

    定义

    #define COLOR(Red, Green, Blue, Alpha) [UIColor colorWithRed:Red/255.0 green:Green/255.0 blue:Blue/255.0 alpha:Alpha]
    

    使用

    COLOR(52, 171, 239, 1.0)
    

    二、UIColor+Extensions(16进制表示RGB)

    1、UIColor+Extensions.h

    #import <UIKit/UIKit.h>
    
    @interface UIColor (Extensions)
    
    + (UIColor *)colorWithHex:(long)hexColor;
    
    + (UIColor *)colorWithHex:(long)hexColor alpha:(CGFloat)alpha;
    
    @end
    

    2、UIColor+Extensions.m

    #import "UIColor+Extensions.h"
    
    @implementation UIColor (Extensions)
    
    + (UIColor *)colorWithHex:(long)hexColor {
        CGFloat red = ((CGFloat)((hexColor & 0xFF0000) >> 16))/255.0f;
        CGFloat green = ((CGFloat)((hexColor & 0xFF00) >> 8))/255.0f;
        CGFloat blue = ((CGFloat)(hexColor & 0xFF))/255.0f;
        return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
    }
    
    + (UIColor *)colorWithHex:(long)hexColor alpha:(CGFloat)alpha{
        CGFloat red = ((CGFloat)((hexColor & 0xFF0000) >> 16))/255.0f;
        CGFloat green = ((CGFloat)((hexColor & 0xFF00) >> 8))/255.0f;
        CGFloat blue = ((CGFloat)(hexColor & 0xFF))/255.0f;
        return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
    }
    
    @end
    

    3、定义和使用

    #define HEXCOLOR(hexColor) [UIColor colorWithHex:hexColor]
    
    HEXCOLOR(0xff2a01)
    

    相关文章

      网友评论

          本文标题:iOS开发 UIColor工具

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