Quantcast
Channel: Notes of a Developer » UIColor
Viewing all articles
Browse latest Browse all 2

UIColor from hex

$
0
0

I added 2 new methods to category UIColor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
+ (UIColor* ) colorWithHex:(int)color {

    float red = (color & 0xff000000) >> 24;
    float green = (color & 0x00ff0000) >> 16;
    float blue = (color & 0x0000ff00) >> 8;
    float alpha = (color & 0x000000ff);

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

+ (UIColor *) colorWithHexRed:(int)red green:(char)green blue:(char)blue alpha:(char)alpha {
    int x = 0;
    x |= (red & 0xff) << 24;
    x |= (green & 0xff) << 16;
    x |= (blue & 0xff) << 8;
    x |= (alpha & 0xff);
    return [UIColor colorWithHex:x];
}

Example:

1
UIColor *redColor = [UIColor colorWithHex:0xff0000ff];

First 6 symbols like HTML color, latest 2 symbols it’s alpha.

The post UIColor from hex appeared first on Notes of a Developer.


Viewing all articles
Browse latest Browse all 2

Trending Articles