-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
415 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// AWKColorHelper.h | ||
// Awkward Helpers | ||
// | ||
// Created by Rens Verhoeven on 27-10-14. | ||
// Copyright (c) 2014 Awkward. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@interface UIColor (AWKColorHelper) | ||
|
||
/** | ||
Transforms the given string (hex) into a UIColor | ||
@param hexString The hex string in one of the following formats: #RGB, #ARGB, #RRGGBB, #AARRGGBB | ||
@return Returns the UIColor that is the closest to the hex string | ||
*/ | ||
+ (UIColor *)colorWithHexString:(NSString *)hexString; | ||
|
||
/** | ||
Get the hex value of the given UIColor | ||
@return Returns the hex in the following format: #RRGGBB | ||
*/ | ||
- (NSString *)hexString; | ||
|
||
/** | ||
Get the hex value of the given UIColor | ||
@return Returns the hex in the following format: #RRGGBB | ||
*/ | ||
- (NSString *)hexValue; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// | ||
// AWKColorHelper.m | ||
// Awkward Helpers | ||
// | ||
// Created by Rens Verhoeven on 27-10-14. | ||
// Copyright (c) 2014 Awkward. All rights reserved. | ||
// | ||
|
||
#import "AWKColorHelper.h" | ||
|
||
@implementation UIColor (AWKColorHelper) | ||
|
||
+ (UIColor *)colorWithHexString:(NSString *)hexString { | ||
NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString]; | ||
CGFloat alpha, red, blue, green; | ||
switch ([colorString length]) { | ||
case 3: // #RGB | ||
alpha = 1.0f; | ||
red = [self colorComponentFrom: colorString start: 0 length: 1]; | ||
green = [self colorComponentFrom: colorString start: 1 length: 1]; | ||
blue = [self colorComponentFrom: colorString start: 2 length: 1]; | ||
break; | ||
case 4: // #ARGB | ||
alpha = [self colorComponentFrom: colorString start: 0 length: 1]; | ||
red = [self colorComponentFrom: colorString start: 1 length: 1]; | ||
green = [self colorComponentFrom: colorString start: 2 length: 1]; | ||
blue = [self colorComponentFrom: colorString start: 3 length: 1]; | ||
break; | ||
case 6: // #RRGGBB | ||
alpha = 1.0f; | ||
red = [self colorComponentFrom: colorString start: 0 length: 2]; | ||
green = [self colorComponentFrom: colorString start: 2 length: 2]; | ||
blue = [self colorComponentFrom: colorString start: 4 length: 2]; | ||
break; | ||
case 8: // #AARRGGBB | ||
alpha = [self colorComponentFrom: colorString start: 0 length: 2]; | ||
red = [self colorComponentFrom: colorString start: 2 length: 2]; | ||
green = [self colorComponentFrom: colorString start: 4 length: 2]; | ||
blue = [self colorComponentFrom: colorString start: 6 length: 2]; | ||
break; | ||
default: | ||
return nil; | ||
break; | ||
} | ||
return [UIColor colorWithRed: red green: green blue: blue alpha: alpha]; | ||
} | ||
|
||
+ (CGFloat)colorComponentFrom:(NSString *)string start:(NSUInteger)start length:(NSUInteger)length { | ||
NSString *substring = [string substringWithRange: NSMakeRange(start, length)]; | ||
NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring]; | ||
unsigned hexComponent; | ||
[[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent]; | ||
return hexComponent / 255.0; | ||
} | ||
|
||
/* Orignal code by: | ||
// | ||
// UIColor+HexColors.m | ||
// KiwiHarness | ||
// | ||
// Created by Tim on 07/09/2012. | ||
// Copyright (c) 2012 Charismatic Megafauna Ltd. All rights reserved. | ||
// | ||
*/ | ||
|
||
- (NSString *)hexString { | ||
return [self hexValue]; | ||
} | ||
|
||
- (NSString *)hexValue { | ||
UIColor *color = self; | ||
|
||
if (!color) { | ||
return nil; | ||
} | ||
|
||
if (color == [UIColor whiteColor]) { | ||
// Special case, as white doesn't fall into the RGB color space | ||
return @"#ffffff"; | ||
} | ||
|
||
CGFloat red; | ||
CGFloat blue; | ||
CGFloat green; | ||
CGFloat alpha; | ||
|
||
[color getRed:&red green:&green blue:&blue alpha:&alpha]; | ||
|
||
int redDec = (int)(red * 255); | ||
int greenDec = (int)(green * 255); | ||
int blueDec = (int)(blue * 255); | ||
|
||
NSString *returnString = [NSString stringWithFormat:@"#%02x%02x%02x", (unsigned int)redDec, (unsigned int)greenDec, (unsigned int)blueDec]; | ||
|
||
return returnString; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// | ||
// AWKStringHelper.h | ||
// Awkward Helpers | ||
// | ||
// Created by Rens Verhoeven on 27-10-14. | ||
// Copyright (c) 2014 Awkward. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
enum { | ||
NSTruncateStringPositionStart=0, | ||
NSTruncateStringPositionMiddle, | ||
NSTruncateStringPositionEnd | ||
}; typedef int NSTruncateStringPosition; | ||
|
||
@interface NSString (AWKStringHelper) | ||
|
||
#pragma mark Hashing methods | ||
|
||
/** | ||
Transforms the current string into a MD5 Hash using CommonCrypto | ||
@return Returns the MD5 hash in a NSString | ||
*/ | ||
- (NSString *)MD5String; | ||
|
||
#pragma mark Checking methods | ||
|
||
/** | ||
Checks if the given string (needle) is found in the current string | ||
@param string The string to look for in the current string | ||
@return Returns YES if the given string is found | ||
*/ | ||
- (BOOL)containsString:(NSString *)string; | ||
|
||
/** | ||
Checks if the given string (needle) is found in the current string | ||
@param string The string to look for in the current string | ||
@param options A set of NSStringCompareOptions to use while searching the current string | ||
@return Returns YES if the given string is found | ||
*/ | ||
- (BOOL)containsString:(NSString *)string options:(NSStringCompareOptions)options; | ||
|
||
#pragma mark Truncation methods | ||
|
||
/** | ||
Truncates the current string to the given length with the HORIZONTAL ELLIPSIS symbol at the end | ||
@param length The length to truncate the string to | ||
@return Returns a new (truncated) string | ||
*/ | ||
- (NSString *)stringByTruncatingToLength:(NSUInteger)length; | ||
|
||
/** | ||
Truncates the current string to the given length with the "HORIZONTAL ELLIPSIS" symbol at the end | ||
@param length The length to truncate the string to | ||
@param truncateFrom The position to put the truncation, start, middle or end | ||
@return Returns a new (truncated) string | ||
*/ | ||
- (NSString *)stringByTruncatingToLength:(NSUInteger)length direction:(NSTruncateStringPosition)truncateFrom; | ||
|
||
/** | ||
Truncates the current string to the given length with the given ellipsis at the end | ||
@param length The length to truncate the string to | ||
@param truncateFrom The position to put the truncation, start, middle or end | ||
@param ellipsis The string to put at the truncation point | ||
@return Returns a new (truncated) string | ||
*/ | ||
- (NSString *)stringByTruncatingToLength:(NSUInteger)length direction:(NSTruncateStringPosition)truncateFrom withEllipsisString:(NSString *)ellipsis; | ||
|
||
#pragma mark Trimming methods | ||
|
||
/** | ||
Removes all the beginning white spaces such as spaces, new lines and tabs of the current string | ||
@return Returns a new (trimmed) string | ||
*/ | ||
- (NSString *)stringByTrimmingLeadingWhitespace; | ||
|
||
/** | ||
Removes all the ending new lines of the current string | ||
@return Returns a new (trimmed) string | ||
*/ | ||
- (NSString *)stringByTrimmingTrailingNewLine; | ||
|
||
/** | ||
Removes all the ending characters in the given set of the current string | ||
@param characterSet The set of characters to trim from the string | ||
@return Returns a new (trimmed) string | ||
*/ | ||
- (NSString *)stringByTrimmingTrailingCharactersInSet:(NSCharacterSet *)characterSet; | ||
|
||
#pragma mark URL Methods | ||
|
||
/** | ||
Creates a URL safe version of the current string | ||
@return Returns a UTF-8 encoded string representation of current string | ||
*/ | ||
- (NSString *)URLEncodedString; | ||
@end |
Oops, something went wrong.