From 36cc8254007f30348af5c9373c5fd13905bf516c Mon Sep 17 00:00:00 2001 From: Rens Verhoeven Date: Mon, 27 Oct 2014 12:50:54 +0100 Subject: [PATCH] Added NSString and UIColor helpers --- AWKHelpers.podspec | 6 +- Classes/AWKColorHelper.h | 36 +++++++++ Classes/AWKColorHelper.m | 98 +++++++++++++++++++++++ Classes/AWKHelpers.h | 4 +- Classes/AWKStringHelper.h | 114 +++++++++++++++++++++++++++ Classes/AWKStringHelper.m | 160 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 415 insertions(+), 3 deletions(-) create mode 100644 Classes/AWKColorHelper.h create mode 100644 Classes/AWKColorHelper.m create mode 100644 Classes/AWKStringHelper.h create mode 100644 Classes/AWKStringHelper.m diff --git a/AWKHelpers.podspec b/AWKHelpers.podspec index e19723c..c8bd999 100644 --- a/AWKHelpers.podspec +++ b/AWKHelpers.podspec @@ -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' @@ -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 diff --git a/Classes/AWKColorHelper.h b/Classes/AWKColorHelper.h new file mode 100644 index 0000000..251c01d --- /dev/null +++ b/Classes/AWKColorHelper.h @@ -0,0 +1,36 @@ +// +// AWKColorHelper.h +// Awkward Helpers +// +// Created by Rens Verhoeven on 27-10-14. +// Copyright (c) 2014 Awkward. All rights reserved. +// + +#import + +@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 diff --git a/Classes/AWKColorHelper.m b/Classes/AWKColorHelper.m new file mode 100644 index 0000000..7e277f3 --- /dev/null +++ b/Classes/AWKColorHelper.m @@ -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 diff --git a/Classes/AWKHelpers.h b/Classes/AWKHelpers.h index 27f5e13..80285a6 100644 --- a/Classes/AWKHelpers.h +++ b/Classes/AWKHelpers.h @@ -8,4 +8,6 @@ #import "AWKDeviceHelper.h" #import "AWKDictionaryHelper.h" -#import "AWKArrayHelper.h" \ No newline at end of file +#import "AWKArrayHelper.h" +#import "AWKColorHelper.h" +#import "AWKStringHelper.h" \ No newline at end of file diff --git a/Classes/AWKStringHelper.h b/Classes/AWKStringHelper.h new file mode 100644 index 0000000..d05496c --- /dev/null +++ b/Classes/AWKStringHelper.h @@ -0,0 +1,114 @@ +// +// AWKStringHelper.h +// Awkward Helpers +// +// Created by Rens Verhoeven on 27-10-14. +// Copyright (c) 2014 Awkward. All rights reserved. +// + +#import + +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 diff --git a/Classes/AWKStringHelper.m b/Classes/AWKStringHelper.m new file mode 100644 index 0000000..d199f05 --- /dev/null +++ b/Classes/AWKStringHelper.m @@ -0,0 +1,160 @@ +// +// AWKStringHelper.m +// Awkward Helpers +// +// Created by Rens Verhoeven on 27-10-14. +// Copyright (c) 2014 Awkward. All rights reserved. +// + +#import "AWKStringHelper.h" +#import + +@implementation NSString (AWKStringHelper) + +/** + Original code by: + + NSStringHelper.m + CocoaHelpers + + Created by Shaun Harrison on 10/14/08. + Copyright (c) 2008-2009 enormego + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + */ + +#pragma mark Hashing methods + +- (NSString*)MD5String { + const char* string = [self UTF8String]; + unsigned char result[16]; + CC_MD5(string, (CC_LONG)strlen(string), result); + NSString* hash = [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", + result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], + result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]]; + + return [hash lowercaseString]; +} + +#pragma mark Checking methods + +- (BOOL)containsString:(NSString*)string { + return [self containsString:string options:NSCaseInsensitiveSearch]; +} + +- (BOOL)containsString:(NSString*)string options:(NSStringCompareOptions)options { + return [self rangeOfString:string options:options].location == NSNotFound ? NO : YES; +} + +#pragma mark Truncation methods + +- (NSString*)stringByTruncatingToLength:(NSUInteger)length { + return [self stringByTruncatingToLength:length direction:NSTruncateStringPositionEnd]; +} + +- (NSString*)stringByTruncatingToLength:(NSUInteger)length direction:(NSTruncateStringPosition)truncateFrom { + return [self stringByTruncatingToLength:length direction:truncateFrom withEllipsisString:@"…"]; +} + +- (NSString*)stringByTruncatingToLength:(NSUInteger)length direction:(NSTruncateStringPosition)truncateFrom withEllipsisString:(NSString*)ellipsis { + NSMutableString *result = [[NSMutableString alloc] initWithString:self]; + NSString *immutableResult; + + if([result length] <= length) { + return self; + } + + NSUInteger charactersEachSide = length / 2; + + NSString* first; + NSString* last; + + switch(truncateFrom) { + case NSTruncateStringPositionStart: + [result insertString:ellipsis atIndex:[result length] - length + [ellipsis length] ]; + immutableResult = [[result substringFromIndex:[result length] - length] copy]; + return immutableResult; + case NSTruncateStringPositionMiddle: + first = [result substringToIndex:charactersEachSide - [ellipsis length]+1]; + last = [result substringFromIndex:[result length] - charactersEachSide]; + immutableResult = [[[NSArray arrayWithObjects:first, last, NULL] componentsJoinedByString:ellipsis] copy]; + return immutableResult; + default: + case NSTruncateStringPositionEnd: + [result insertString:ellipsis atIndex:length - [ellipsis length]]; + immutableResult = [[result substringToIndex:length] copy]; + return immutableResult; + } +} + +#pragma mark Trimming methods + +-(NSString*)stringByTrimmingLeadingWhitespace { + NSInteger i = 0; + + while ((i < [self length]) + && [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[self characterAtIndex:i]]) { + i++; + } + return [self substringFromIndex:i]; +} + +-(NSString*)stringByTrimmingTrailingNewLine { + return [self stringByTrimmingTrailingCharactersInSet:[NSCharacterSet newlineCharacterSet]]; +} + +- (NSString *)stringByTrimmingTrailingCharactersInSet:(NSCharacterSet *)characterSet { + NSUInteger location = 0; + NSUInteger length = [self length]; + unichar charBuffer[length]; + [self getCharacters:charBuffer]; + + for (length = [self length]; length > 0; length--) { + if (![characterSet characterIsMember:charBuffer[length - 1]]) { + break; + } + } + + return [self substringWithRange:NSMakeRange(location, length - location)]; +} + +#pragma mark URL Methods + +- (NSString *)URLEncodedString { + NSString *string = [NSString stringWithFormat:@"%@", self]; + NSMutableString *output = [NSMutableString string]; + const unsigned char *source = (const unsigned char *)[string UTF8String]; + NSInteger sourceLen = strlen((const char *)source); + for (int i = 0; i < sourceLen; ++i) { + const unsigned char thisChar = source[i]; + if (thisChar == ' '){ + [output appendString:@"+"]; + } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' || thisChar == ':' || thisChar == ';' || + (thisChar >= 'a' && thisChar <= 'z') || + (thisChar >= 'A' && thisChar <= 'Z') || + (thisChar >= '0' && thisChar <= '9')) { + [output appendFormat:@"%c", thisChar]; + } else { + [output appendFormat:@"%%%02X", thisChar]; + } + } + return output; +} + +@end