-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIColor+Helper.m
58 lines (46 loc) · 1.75 KB
/
UIColor+Helper.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// UIColor+Helper.m
// IRPasscode
//
// Created by Phil on 2019/11/15.
// Copyright © 2019 Phil. All rights reserved.
//
#import "UIColor+Helper.h"
@implementation UIColor (Helper)
+ (UIColor *)colorWithColorCodeString:(NSString *)colorString {
unsigned color = 0;
NSScanner *scanner = [NSScanner scannerWithString:colorString];
// [scanner setScanLocation:1]; // bypass '#' character
[scanner scanHexInt:&color];
if(colorString.length==6){
return [UIColor colorWithRGB:color];
}else{
return [UIColor colorWithARGB:color];
}
}
+ (UIColor *)colorWithARGB:(NSUInteger)color {
return [UIColor colorWithRed:((color >> 16) & 0xFF) / 255.0f
green:((color >> 8) & 0xFF) / 255.0f
blue:((color >> 0) & 0xFF) / 255.0f
alpha:((color >> 24) & 0xFF) / 255.0f];
}
+ (UIColor *)colorWithRGB:(NSUInteger)color {
return [UIColor colorWithRed:((color >> 16) & 0xFF) / 255.0f
green:((color >> 8) & 0xFF) / 255.0f
blue:((color >> 0) & 0xFF) / 255.0f
alpha:255.0f];
}
+ (UIColor *)colorWithRGBA:(NSUInteger)color {
return [UIColor colorWithRed:((color >> 24) & 0xFF) / 255.0f
green:((color >> 16) & 0xFF) / 255.0f
blue:((color >> 8) & 0xFF) / 255.0f
alpha:((color) & 0xFF) / 255.0f];
}
+ (UIColor *)colorWithRGBWithString:(NSString *)colorString {
unsigned color = 0;
NSScanner *scanner = [NSScanner scannerWithString:colorString];
// [scanner setScanLocation:1]; // bypass '#' character
[scanner scanHexInt:&color];
return [UIColor colorWithRGB:color];
}
@end