- First Commit

This commit is contained in:
giuseppenuc committed 2016-02-09 10:41:40 +01:00
1 parent f04f8121a1
commit a4d16c169a
224 files changed
+13535 -40

No files matched your search

+15
View File
@@ -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
+54
View File
@@ -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