Skip to content

Instantly share code, notes, and snippets.

@dsibilly
Created June 21, 2011 18:20
Show Gist options
  • Save dsibilly/1038500 to your computer and use it in GitHub Desktop.
Save dsibilly/1038500 to your computer and use it in GitHub Desktop.
Objective-C category demonstration
//
// 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;
/**
- (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 NSString with the original NSString's contents reversed
*/
- (NSString*)reverse;
@end
//
// 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
@nevyn
Copy link

nevyn commented Jul 3, 2013

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment