Skip to content

Instantly share code, notes, and snippets.

View indragiek's full-sized avatar

Indragie Karunaratne indragiek

View GitHub Profile
@interface NSString (Additions)
- (NSString*)stringByRemovingWhitespace;
@property (readonly) NSString *MD5;
@end
@implementation NSString (Additions)
- (NSString*)stringByRemovingWhitespace
{
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
@indragiek
indragiek / ColorOverlay.m
Created May 3, 2011 03:30
Replicating Photoshop Layer Styles Using Core Graphics
CGContextRef ctx = UIGraphicsGetCurrentContext(); // your drawing context
CGRect colorRect = CGRectMake(0, 0, 100, 100); // the rect to draw the color overlay into
// Set the blend mode to "Normal"
// This is the default blend mode setting so this line is not required, shown here for demonstration
CGContextSetBlendMode(ctx, kCGBlendModeNormal);
// Set the opacity of the drawing context (once again, not required)
CGContextSetAlpha(ctx, 1.0);
// Create the red color
CGColorRef redColor = CGColorCreateGenericRGB(0.908, 0.149, 0.145, 1.000);
// Set the fill color of the context and fill the rect
@indragiek
indragiek / NSView-SNRAdditions.m
Created September 11, 2011 03:11
Animated NSView redraws + scrolling
@implementation NSView (SNRAdditions)
- (void)scrollPointAnimated:(NSPoint)point
{
NSScrollView *scrollView = [self enclosingScrollView];
NSClipView *clipView = [scrollView contentView];
NSPoint constrainedPoint = [clipView constrainScrollPoint:point];
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[clipView animator] setBoundsOrigin:constrainedPoint];
[NSAnimationContext endGrouping];
@indragiek
indragiek / libxml2SAX.m
Created November 6, 2011 19:30
libxml2 parsing code
- (BOOL)parse
{
NSLog(@"Began parsing");
[inputStream open];
/* Read 4 bytes initially to create the push parser context and begin parsing */
NSStreamStatus status = [inputStream streamStatus];
uint8_t buf[4];
NSInteger bytesRead;
while (status != NSStreamStatusAtEnd) {
if (status == NSStreamStatusError) {
@indragiek
indragiek / libxmlFileInputStream.m
Created November 8, 2011 02:39
libxml2 SAX + file input stream
- (BOOL)parse
{
NSArray *libraryDatabases = [[[NSUserDefaults standardUserDefaults] persistentDomainForName:@"com.apple.iApps"] objectForKey:@"iTunesRecentDatabases"];
NSURL *libraryURL = (([libraryDatabases count])) ? [NSURL URLWithString:[libraryDatabases objectAtIndex:0]] : nil;
assert(libraryURL);
const char *libPath = [libraryURL.path UTF8String];
const int min = 4;
FILE *fp = fopen(libPath, "r");
assert(fp);
char buffer[min];
@indragiek
indragiek / AmazonArtwork.m
Created November 19, 2011 00:27
Retrieving album artwork from Amazon's Product Services API in Cocoa (using AFNetworking)
static NSString* const kAWSAccessKey = @"__ACCESS__KEY__"; // From AWS Developer portal
static NSString* const kAWSSecretKey = @"__SECRET__KEY__"; // From AWS Developer portal
static NSString* const kAWSAssociateTag = @"examp-99"; // Can be a bogus tag in the format xxxxx-XX (x being a letter and X being a number)
/* NSString category for HMAC and URL string encoding */
@interface NSString (AWSAdditions)
- (NSString*)URLEncodedStringForCharacters:(NSString*)characters;
+ (NSData*)HMACSHA256EncodedDataWithKey:(NSString*)key data:(NSString*)data;
@end
@indragiek
indragiek / BadgeCount.m
Created November 19, 2011 20:04
Automatically updating NSDockTile with badge count
@interface AppDelegate : NSObject
@property (nonatomic, assign) NSInteger badgeCount;
@end
@implementation AppDelegate
@synthesize badgeCount;
// Override the badgeCount setter to update the dock tile whenever the count is set
- (void)setBadgeCount:(NSInteger)count
{
@indragiek
indragiek / Autoscroll.m
Created November 19, 2011 20:15
AppKit autoscroll implemented in UIKit
@interface UIView (Autoscroll)
- (void)autoscroll:(UITouch*)touch;
@end
@implementation UIView (Autoscroll)
- (void)autoscroll:(UITouch*)touch
{
// Pass the autoscroll messages onto the superview until it reaches a view that handles the message
// If you want to pass the messages through the responder chain then this category would need to be
// implemented on UIResponder instead of UIView and you would call [self.nextResponder autoscroll:touch]
@indragiek
indragiek / gist:1397046
Created November 27, 2011 05:50
NSWindow -isFullscreen
@interface NSWindow (Fullscreen)
- (BOOL)isFullscreen;
@end
@implementation NSWindow (Fullscreen)
- (BOOL)isFullscreen
{
return ([self styleMask] & NSFullScreenWindowMask) == NSFullScreenWindowMask;
}
@end
@indragiek
indragiek / NSWindow+Fade.h
Created November 27, 2011 05:55
NSWindow+Fade - Animator proxy based NSWindow fading
@interface NSWindow (Fade)
- (IBAction)fadeIn:(id)sender;
- (IBAction)fadeOut:(id)sender;
@end