Created
June 21, 2011 18:20
-
-
Save dsibilly/1038500 to your computer and use it in GitHub Desktop.
Revisions
-
dsibilly revised this gist
May 26, 2012 . 4 changed files with 83 additions and 315 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,49 +1,49 @@ // // NSString+UsefulStuff.h // // Duane Sibilly <duane@sibilly.com> // 6/21/11 // Copyright (c) 2011-2012 Duane Sibilly. All rights reserved. #import <Foundation/Foundation.h> // The use of the parenthesis in the interface declaration is what tells the // compiler that this is a category on the NSString class instead of a // redefinition of NSString. @interface NSString (UsefulStuff) /** + (NSString*)stringTruncatedToWidth:withString:andFont: Generates a truncated copy of the given NSString, truncated to the desired width for the given typeface and size. width - A CGFloat representing the desired width of the truncated NSString. string - An NSString object with the content to be truncated. font - A UIFont object representing the desired typeface and font size. Example: NSString *message = @"Can you hear this long-winded message?"; UIFont *messageFont = [UIFont fontWithName:@"Marker Felt" size:32]; NSString *output = [NSString stringTruncatedToWidth:48.0f withString:message andFont:messageFont]; Returns an NSString containing the truncated string, followed by an ellipsis. */ + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont:(UIFont*)font; /** + (BOOL)stringIsPalindrome: Deterines if a provided NSString is a palindrome. aString - The NSString to be tested. Example: [NSString stringIsPalindrome:@"RADAR"]; // return YES Returns a BOOL cooresponding to the NSString palindrome status. */ + (BOOL)stringIsPalindrome:(NSString*)aString; @@ -52,10 +52,10 @@ /** - (NSString*)MD5Hash Generates an MD5 cryptographic hash of this NSString's contents Example: NSString *hash = [@"The quick brown fox jumped over the lazy dog" MD5Hash]; Returns an NSString containing the hexidecimal representation of the MD5 hash. */ - (NSString*)MD5Hash; @@ -65,16 +65,16 @@ - (NSString*)truncateToWidth:withFont: Generates an NSString truncated to the indicated width for a given a typeface and size. width - A CGFloat representing the desired width of the truncated NSString. font - A UIFont object representing the desired typeface and font size. Example: NSString *testString = @"This string is too damn long!" [testString truncateToWidth:64.0f withFont:[UIFont fontWithName:@"Helvetica" size:28]]; Returns an NSString containing the truncated string, followed by an ellipsis. */ - (NSString*)truncateToWidth:(CGFloat)width withFont:(UIFont*)font; @@ -83,26 +83,26 @@ /** - (BOOL)isPalindrome Determines whether this string is a palindrome. Example: [@"HANNAH" isPalindrome]; // returns YES [@"CLAUDE" isPalindrome]; // returns NO Returns a BOOL corresponding to this NSString's palindrome status. */ - (BOOL)isPalindrome; /** -(NSString*)reverse Reverses the contents of this NSString. Example: NSString *testString = @"stressed"; NSString *testReversed = [testString reverse]; // @"desserts" Returns an NSString with the original NSString's contents reversed */ - (NSString*)reverse; @end This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,125 +1,126 @@ // // NSString+UsefulStuff.m // // Duane Sibilly <duane@sibilly.com> // 6/21/11 // Copyright (c) 2011-2012 Duane Sibilly. All rights reserved. #import "NSString+UsefulStuff.h" #import <CommonCrypto/CommonDigest.h> #define ELLIPSIS @"..." @interface NSString (UsefulStuffPrivate) +(BOOL) stringIsPalindrome:(NSString *)aString position:(NSInteger)position; @end @implementation NSString (UsefulStuff) +(NSString*) stringTruncatedToWidth:(CGFloat)width withString:(NSString *)string andFont:(UIFont *)font { return [string truncateToWidth:width withFont:font]; } +(BOOL) stringIsPalindrome:(NSString *)aString { return [NSString stringIsPalindrome:aString position:0]; } +(BOOL) stringIsPalindrome:(NSString *)aString position:(NSInteger)position { NSString *_string = [NSString stringWithString:aString]; NSInteger _position = position; if (! _string) { return NO; } NSInteger stringLength = [_string length]; NSString *firstChar = [[_string substringToIndex:_position] substringToIndex:1]; NSString *lastChar = [[_string substringToIndex:(stringLength - 1 - _position)] substringToIndex:1]; if (_position > (stringLength / 2)) { return YES; } if (! [firstChar isEqualToString:lastChar]) { return NO; } return [NSString stringIsPalindrome:_string position:(_position + 1)]; } -(NSString*) MD5Hash { // Create a C-style pointer to the UT8-encoded contents of the NSString const char *pointer = [self UTF8String]; // Create a buffer array big enough to hold the digest unsigned char buffer[CC_MD5_DIGEST_LENGTH]; // Create 16-byte MD5 hash value, store in buffer // See: CC_MD5(3cc) manpage on OS X & iOS. CC_MD5(pointer, strlen(pointer), buffer); // Convert MD5 digest in buffer to an autoreleased NSString of hexidecimal // values. NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i += 1) { [result appendFormat:@"%02x", buffer[i]]; } return [result copy]; } -(NSString*) truncateToWidth:(CGFloat)width withFont:(UIFont *)font { // Obtain a mutable copy of this NSString. NSMutableString *truncatedString = [self mutableCopy]; // If this NSString is longer than the desired width, truncate. if ([self sizeWithFont:font].width > width) { // Subtract an ellipsis' worth of width from the desired width to obtain the // truncation width. width -= [ELLIPSIS sizeWithFont:font].width; // While the string is longer than the truncation width, remove characters // from the end of the string. NSRange range = {truncatedString.length - 1, 1}; while ([truncatedString sizeWithFont:font].width > width) { [truncatedString deleteCharactersInRange:range]; range.location -= 1; } // Once truncation is complete, append an ellipsis to the end of the string. [truncatedString replaceCharactersInRange:range withString:ELLIPSIS]; } return [truncatedString copy]; } -(BOOL) isPalindrome { return [NSString stringIsPalindrome:self]; } -(NSString*) reverse { NSMutableString *reversedString = [NSMutableString stringWithCapacity:[self length]]; for (int i = ([self length] - 1); i >= 0; i -= 1) { [reversedString appendString:[NSString stringWithFormat:@"%C", [self characterAtIndex:i]]]; } return reversedString; } @end This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,108 +0,0 @@ This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,125 +0,0 @@ -
dsibilly revised this gist
May 25, 2012 . No changes.There are no files selected for viewing
-
dsibilly revised this gist
May 25, 2012 . 2 changed files with 233 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,108 @@ // // NSString+UsefulShit.h // // Created by Duane Sibilly on 6/21/11. // Copyright 2011 Duane Sibilly. All rights reserved. // #import <Foundation/Foundation.h> // The use of the parenthesis in the interface declaration is what tells the // compiler that this is a category on the NSString class instead of a // redefinition of NSString. @interface NSString (UsefulShit) /** + (NSString*)stringTruncatedToWidth:withString:andFont: Generates a truncated copy of the given NSString, truncated to the desired width for the given typeface and size. width - A CGFloat representing the desired width of the truncated NSString. string - An NSString object with the content to be truncated. font - A UIFont object representing the desired typeface and font size. Example: NSString *message = @"Can you hear this long-winded message?"; UIFont *messageFont = [UIFont fontWithName:@"Marker Felt" size:32]; NSString *output = [NSString stringTruncatedToWidth:48.0f withString:message andFont:messageFont]; Returns an NSString containing the truncated string, followed by an ellipsis. */ + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont:(UIFont*)font; /** + (BOOL)stringIsPalindrome: Deterines if a provided NSString is a palindrome. aString - The NSString to be tested. Example: [NSString stringIsPalindrome:@"RADAR"]; // return YES Returns a BOOL cooresponding to the NSString palindrome status. */ + (BOOL)stringIsPalindrome:(NSString*)aString; /** - (NSString*)MD5Hash Generates an MD5 cryptographic hash of this NSString's contents Example: NSString *hash = [@"The quick brown fox jumped over the lazy dog" MD5Hash]; Returns an NSString containing the hexidecimal representation of the MD5 hash. */ - (NSString*)MD5Hash; /** - (NSString*)truncateToWidth:withFont: Generates an NSString truncated to the indicated width for a given a typeface and size. width - A CGFloat representing the desired width of the truncated NSString. font - A UIFont object representing the desired typeface and font size. Example: NSString *testString = @"This string is too damn long!" [testString truncateToWidth:64.0f withFont:[UIFont fontWithName:@"Helvetica" size:28]]; Returns an NSString containing the truncated string, followed by an ellipsis. */ - (NSString*)truncateToWidth:(CGFloat)width withFont:(UIFont*)font; /** - (BOOL)isPalindrome Determines whether this string is a palindrome. Example: [@"HANNAH" isPalindrome]; // returns YES [@"CLAUDE" isPalindrome]; // returns NO Returns a BOOL corresponding to this NSString's palindrome status. */ - (BOOL)isPalindrome; /** -(NSString*)reverse Reverses the contents of this NSString. Example: NSString *testString = @"stressed"; NSString *testReversed = [testString reverse]; // @"desserts" Returns an autoreleased NSString with the original NSString's contents reversed */ - (NSString*)reverse; @end This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,125 @@ // // NSString+UsefulShit.m // // Created by Duane Sibilly on 6/21/11. // Copyright 2011 Duane Sibilly. All rights reserved. // #import "NSString+UsefulShit.h" #import <CommonCrypto/CommonDigest.h> #define ELLIPSIS @"..." // Like in the interface, the use of parentheses in the declaration implies that // this is a category to an existing class. @implementation NSString (UsefulShit) + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont:(UIFont*)font { // Since the instance method truncateToWidth:withFont: already returns an // autoreleased truncated string, simply call that method on the supplied // NSString. return [string truncateToWidth:width withFont:font]; } + (BOOL)stringIsPalindrome:(NSString*)aString { return [NSString stringIsPalindrome:aString position:0]; } // Undeclared helper class method for use with +(BOOL)stringIsPalindrome: and // -(BOOL)isPalindrome. + (BOOL)stringIsPalindrome:(NSString*)aString position:(NSInteger)position { NSString *_string = [NSString stringWithString:aString]; NSInteger _position = position; if (! _string) { return NO; } NSInteger stringLength = [_string length]; NSString *firstChar = [[_string substringFromIndex:_position] substringToIndex:1]; NSString *lastChar = [[_string substringFromIndex:(stringLength - 1 - _position)] substringToIndex:1]; if (_position > (stringLength / 2)) { return YES; } if (! [firstChar isEqualToString:lastChar]) { return NO; } return [NSString stringIsPalindrome:_string position:(_position + 1)]; } - (NSString*)MD5Hash { // Create a C-style pointer to the UT8-encoded contents of the NSString const char *pointer = [self UTF8String]; // Create a buffer array big enough to hold the digest unsigned char buffer[CC_MD5_DIGEST_LENGTH]; // Create 16-byte MD5 hash value, store in buffer // See: CC_MD5(3cc) manpage on OS X & iOS. CC_MD5(pointer, strlen(pointer), buffer); // Convert MD5 digest in buffer to an autoreleased NSString of hexidecimal // values. NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i += 1) { [result appendFormat:@"%02x", buffer[i]]; } return output; } - (NSString*)truncateToWidth:(CGFloat)width withFont:(UIFont*)font { // Obtain a mutable copy of this NSString. NSMutableString *truncatedString = [self mutableCopy]; // If this NSString is longer than the desired width, truncate. if ([self sizeWithFont:font].width > width) { // Subtract an ellipsis' worth of width from the desired width to obtain the // truncation width. width -= [ELLIPSIS sizeWithFont:font].width; // While the string is longer than the truncation width, remove characters // from the end of the string. NSRange range = {truncatedString.length - 1, 1} while ([truncatedString sizeWithFont:font].width > width) { [truncatedString deleteCharactersInRange:range]; range.location -= 1; } // Once truncation is complete, append an ellipsis to the end of the string. [truncatedString replaceCharactersInRange:range withString:ELLIPSIS]; } return [truncatedString autorelease]; } - (BOOL)isPalindrome { return [NSString stringIsPalindrome:self]; } - (NSString*)reverse { NSMutableString *reversedString = [NSMutableString stringWithCapacity:[self length]]; for (int i = ([self length] - 1); i >= 0; i -= 1) { [reversedString appendString:[NSString stringWithFormat:@"%C", [self characterAtIndex:i]]]; } return reversedString; } @end -
dsibilly revised this gist
Jun 21, 2011 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,10 @@ // // NSString+UsefulShit.h // // Created by Duane Sibilly on 6/21/11. // Copyright 2011 Duane Sibilly. All rights reserved. // #import <Foundation/Foundation.h> // The use of the parenthesis in the interface declaration is what tells the -
dsibilly revised this gist
Jun 21, 2011 . 2 changed files with 156 additions and 64 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,101 @@ #import <Foundation/Foundation.h> // The use of the parenthesis in the interface declaration is what tells the // compiler that this is a category on the NSString class instead of a // redefinition of NSString. @interface NSString (UsefulShit) /** + (NSString*)stringTruncatedToWidth:withString:andFont: Generates a truncated copy of the given NSString, truncated to the desired width for the given typeface and size. width - A CGFloat representing the desired width of the truncated NSString. string - An NSString object with the content to be truncated. font - A UIFont object representing the desired typeface and font size. Example: NSString *message = @"Can you hear this long-winded message?"; UIFont *messageFont = [UIFont fontWithName:@"Marker Felt" size:32]; NSString *output = [NSString stringTruncatedToWidth:48.0f withString:message andFont:messageFont]; Returns an NSString containing the truncated string, followed by an ellipsis. */ + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont:(UIFont*)font; /** + (NSString*)stringIsPalindrome: Deterines if a provided NSString is a palindrome. aString - The NSString to be tested. Example: [NSString stringIsPalindrome:@"RADAR"]; // return YES Returns a BOOL cooresponding to the NSString palindrome status. */ + (BOOL)stringIsPalindrome:(NSString*)aString; /** - (NSString*)MD5Hash Generates an MD5 cryptographic hash of this NSString's contents Example: NSString *hash = [@"The quick brown fox jumped over the lazy dog" MD5Hash]; Returns an NSString containing the hexidecimal representation of the MD5 hash. */ - (NSString*)MD5Hash; /** - (NSString*)truncateToWidth:withFont: Generates an NSString truncated to the indicated width for a given a typeface and size. width - A CGFloat representing the desired width of the truncated NSString. font - A UIFont object representing the desired typeface and font size. Example: NSString *testString = @"This string is too damn long!" [testString truncateToWidth:64.0f withFont:[UIFont fontWithName:@"Helvetica" size:28]]; Returns an NSString containing the truncated string, followed by an ellipsis. */ - (NSString*)truncateToWidth:(CGFloat)width withFont:(UIFont*)font; /** - (BOOL)isPalindrome Determines whether this string is a palindrome. Example: [@"HANNAH" isPalindrome]; // returns YES [@"CLAUDE" isPalindrome]; // returns NO Returns a BOOL corresponding to this NSString's palindrome status. */ - (BOOL)isPalindrome; /** -(NSString*)reverse Reverses the contents of this NSString. Example: NSString *testString = @"stressed"; NSString *testReversed = [testString reverse]; // @"desserts" Returns an autoreleased NSString with the original NSString's contents reversed */ - (NSString*)reverse; @end This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,75 +4,17 @@ // Created by Duane Sibilly on 6/21/11. // Copyright 2011 Duane Sibilly. All rights reserved. // #import "NSString+UsefulShit.h" #import <CommonCrypto/CommonDigest.h> #define ELLIPSIS @"..." // Like in the interface, the use of parentheses in the declaration implies that // this is a category to an existing class. @implementation NSString (UsefulShit) + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont:(UIFont*)font { // Since the instance method truncateToWidth:withFont: already returns an // autoreleased truncated string, simply call that method on the supplied @@ -81,7 +23,38 @@ + (NSString*)stringTruncatedToWidth:(CGFloat)width } + (BOOL)stringIsPalindrome:(NSString*)aString { return [NSString stringIsPalindrome:aString position:0]; } // Undeclared helper class method for use with +(BOOL)stringIsPalindrome: and // -(BOOL)isPalindrome. + (BOOL)stringIsPalindrome:(NSString*)aString position:(NSInteger)position { NSString *_string = [NSString stringWithString:aString]; NSInteger _position = position; if (! _string) { return NO; } NSInteger stringLength = [_string length]; NSString *firstChar = [[_string substringFromIndex:_position] substringToIndex:1]; NSString *lastChar = [[_string substringFromIndex:(stringLength - 1 - _position)] substringToIndex:1]; if (_position > (stringLength / 2)) { return YES; } if (! [firstChar isEqualToString:lastChar]) { return NO; } return [NSString stringIsPalindrome:_string position:(_position + 1)]; } - (NSString*)MD5Hash { // Create a C-style pointer to the UT8-encoded contents of the NSString const char *pointer = [self UTF8String]; @@ -96,7 +69,7 @@ - (NSString*)MD5Hash // Convert MD5 digest in buffer to an autoreleased NSString of hexidecimal // values. NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i += 1) { [result appendFormat:@"%02x", buffer[i]]; } @@ -131,4 +104,22 @@ - (NSString*)truncateToWidth:(CGFloat)width withFont:(UIFont*)font return [truncatedString autorelease]; } - (BOOL)isPalindrome { return [NSString stringIsPalindrome:self]; } - (NSString*)reverse { NSMutableString *reversedString = [NSMutableString stringWithCapacity:[self length]]; for (int i = ([self length] - 1); i >= 0; i -= 1) { [reversedString appendString:[NSString stringWithFormat:@"%C", [self characterAtIndex:i]]]; } return reversedString; } @end -
dsibilly revised this gist
Jun 21, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -96,7 +96,7 @@ - (NSString*)MD5Hash // Convert MD5 digest in buffer to an autoreleased NSString of hexidecimal // values. NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i += 1) { [result appendFormat:@"%02x", buffer[i]]; } -
dsibilly revised this gist
Jun 21, 2011 . 1 changed file with 6 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -34,7 +34,9 @@ + (NSString*)stringTruncatedToWidth:withString:andFont: Returns an NSString containing the truncated string, followed by an ellipsis. */ + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont:(UIFont*)font; /** - (NSString*)MD5Hash @@ -68,7 +70,9 @@ - (NSString*)truncateToWdith:(CGFloat)width withFont:(UIFont*)font; // declaration implies that this is a category to an existing class. @implementation NSString (UsefulShit) + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont:(UIFont*)font { // Since the instance method truncateToWidth:withFont: already returns an // autoreleased truncated string, simply call that method on the supplied -
dsibilly revised this gist
Jun 21, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -34,7 +34,7 @@ + (NSString*)stringTruncatedToWidth:withString:andFont: Returns an NSString containing the truncated string, followed by an ellipsis. */ + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont:(UIFont*)font; /** - (NSString*)MD5Hash -
dsibilly created this gist
Jun 21, 2011 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,130 @@ // // NSString+UsefulShit.m // // Created by Duane Sibilly on 6/21/11. // Copyright 2011 Duane Sibilly. All rights reserved. // // // Objective-C category demonstration // I've put the interface and implementation in the same file for the sake of // easy readability in a single gist. // #import <Foundation/Foundation.h> #import <CommonCrypto/CommonDigest.h> #define ELLIPSIS @"..." /* START CATEGORY INTERFACE */ // The @interface would normally go in the header file, as in other C-like // languages. The use of the parenthesis in the interface declaration is what // tells the compiler that this is a category on the NSString class instead of a // redefinition of NSString. @interface NSString (UsefulShit) /** + (NSString*)stringTruncatedToWidth:withString:andFont: Generates a truncated copy of the given NSString, truncated to the desired width for the given typeface and size. width - A CGFloat representing the desired width of the truncated NSString. string - An NSString object with the content to be truncated. font - A UIFont object representing the desired typeface and font size. Returns an NSString containing the truncated string, followed by an ellipsis. */ + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont(UIFont*)font; /** - (NSString*)MD5Hash Generates an MD5 cryptographic hash of this NSString's contents Returns an NSString containing the hexidecimal representation of the MD5 hash. */ - (NSString*)MD5Hash; /** Instance: truncateToWidth:withFont: Generates an NSString truncated to the indicated width for a given a typeface and size. width - A CGFloat representing the desired width of the truncated NSString. font - A UIFont object representing the desired typeface and font size. Returns an NSString containing the truncated string, followed by an ellipsis. */ - (NSString*)truncateToWdith:(CGFloat)width withFont:(UIFont*)font; @end /* END CATEGORY INTERFACE */ /* START CATEGORY IMPLEMENTATION */ // The implementation would usually live on its own in a .m file (.m is the // traditional file extension for Objective-C source, much like .c for C and // .cpp for C++. Like in the interface, the use of parentheses in the // declaration implies that this is a category to an existing class. @implementation NSString (UsefulShit) + (NSString*)stringTruncatedToWidth:(CGFloat)width withString:(NSString*)string andFont:(UIFont*)font { // Since the instance method truncateToWidth:withFont: already returns an // autoreleased truncated string, simply call that method on the supplied // NSString. return [string truncateToWidth:width withFont:font]; } - (NSString*)MD5Hash { // Create a C-style pointer to the UT8-encoded contents of the NSString const char *pointer = [self UTF8String]; // Create a buffer array big enough to hold the digest unsigned char buffer[CC_MD5_DIGEST_LENGTH]; // Create 16-byte MD5 hash value, store in buffer // See: CC_MD5(3cc) manpage on OS X & iOS. CC_MD5(pointer, strlen(pointer), buffer); // Convert MD5 digest in buffer to an autoreleased NSString of hexidecimal // values. NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i += 1) { [result appendFormat:@"%02x", buffer[i]]; } return output; } - (NSString*)truncateToWidth:(CGFloat)width withFont:(UIFont*)font { // Obtain a mutable copy of this NSString. NSMutableString *truncatedString = [self mutableCopy]; // If this NSString is longer than the desired width, truncate. if ([self sizeWithFont:font].width > width) { // Subtract an ellipsis' worth of width from the desired width to obtain the // truncation width. width -= [ELLIPSIS sizeWithFont:font].width; // While the string is longer than the truncation width, remove characters // from the end of the string. NSRange range = {truncatedString.length - 1, 1} while ([truncatedString sizeWithFont:font].width > width) { [truncatedString deleteCharactersInRange:range]; range.location -= 1; } // Once truncation is complete, append an ellipsis to the end of the string. [truncatedString replaceCharactersInRange:range withString:ELLIPSIS]; } return [truncatedString autorelease]; } @end