Created
June 21, 2011 18:20
-
-
Save dsibilly/1038500 to your computer and use it in GitHub Desktop.
Objective-C category demonstration
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 characters
| // | |
| // 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The class methods really should be instance methods. Selectors shouldn't have parts beginning with "and". Category methods on code you don't own really should start with a prefix, or apps will break in mysterious ways when the original class gain methods with the same names.