- First Commit
This commit is contained in:
1 parent
f04f8121a1
commit
a4d16c169a
224 files changed
+13535
-40
No files matched your search
Executable
+15
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// NSString+HexColor.h
|
||||
// Packman
|
||||
//
|
||||
// Created by Giuseppe Nucifora on 20/10/12.
|
||||
// Copyright (c) 2015 Purplenetwork S.r.l. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface NSString (HexColor)
|
||||
|
||||
- (UIColor*)colorFromHex;
|
||||
|
||||
@end
|
||||
Executable
+54
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// NSString+HexColor.m
|
||||
// Packman
|
||||
//
|
||||
// Created by Giuseppe Nucifora on 20/10/12.
|
||||
// Copyright (c) 2012 Purplenetwork S.r.l. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NSString+HexColor.h"
|
||||
|
||||
@implementation NSString (HexColor)
|
||||
|
||||
- (UIColor*)colorFromHex
|
||||
{
|
||||
NSString *hexColor = [[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
|
||||
|
||||
if ([hexColor length] < 6)
|
||||
return [UIColor blackColor];
|
||||
if ([hexColor hasPrefix:@"#"])
|
||||
hexColor = [hexColor substringFromIndex:1];
|
||||
if ([hexColor length] != 6 && [hexColor length] != 8)
|
||||
return [UIColor blackColor];
|
||||
|
||||
NSRange range;
|
||||
range.location = 0;
|
||||
range.length = 2;
|
||||
|
||||
NSString *rString = [hexColor substringWithRange:range];
|
||||
|
||||
range.location = 2;
|
||||
NSString *gString = [hexColor substringWithRange:range];
|
||||
|
||||
range.location = 4;
|
||||
NSString *bString = [hexColor substringWithRange:range];
|
||||
|
||||
range.location = 6;
|
||||
NSString *aString = @"FF";
|
||||
if ([hexColor length] == 8)
|
||||
aString = [hexColor substringWithRange:range];
|
||||
|
||||
// Scan values
|
||||
unsigned int r, g, b, a;
|
||||
[[NSScanner scannerWithString:rString] scanHexInt:&r];
|
||||
[[NSScanner scannerWithString:gString] scanHexInt:&g];
|
||||
[[NSScanner scannerWithString:bString] scanHexInt:&b];
|
||||
[[NSScanner scannerWithString:aString] scanHexInt:&a];
|
||||
|
||||
return [UIColor colorWithRed:((float) r / 255.0f)
|
||||
green:((float) g / 255.0f)
|
||||
blue:((float) b / 255.0f)
|
||||
alpha:((float) a / 255.0f)];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user