Skip to content

Commit

Permalink
Added NSString and UIColor helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
renssies committed Oct 27, 2014
1 parent e228ec1 commit 36cc825
Show file tree
Hide file tree
Showing 6 changed files with 415 additions and 3 deletions.
6 changes: 4 additions & 2 deletions AWKHelpers.podspec
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@

Pod::Spec.new do |s|
s.name = "AWKHelpers"
s.version = "0.1.1"
s.version = "0.2"
s.summary = "A growing collection of UIKit and Foundation categories (helpers)"
s.description = <<-DESC
A growing collection of UIKit and Foundation categories (helpers). Support includes:
- NSDictionary
- NSArray
- NSString
- UIDevice
- UIColor
DESC
s.homepage = "https://github.com/awkward/AWKHelpers"
s.license = 'MIT'
Expand All @@ -21,5 +23,5 @@ Pod::Spec.new do |s|
s.source_files = 'Classes'

s.public_header_files = 'Classes/**/*.h'
s.frameworks = 'UIKit', 'Healthkit', 'Foundation'
s.frameworks = 'UIKit', 'HealthKit', 'Foundation', 'security'
end
36 changes: 36 additions & 0 deletions Classes/AWKColorHelper.h
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
98 changes: 98 additions & 0 deletions Classes/AWKColorHelper.m
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
4 changes: 3 additions & 1 deletion Classes/AWKHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@

#import "AWKDeviceHelper.h"
#import "AWKDictionaryHelper.h"
#import "AWKArrayHelper.h"
#import "AWKArrayHelper.h"
#import "AWKColorHelper.h"
#import "AWKStringHelper.h"
114 changes: 114 additions & 0 deletions Classes/AWKStringHelper.h
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
Loading

0 comments on commit 36cc825

Please sign in to comment.