Skip to content

Instantly share code, notes, and snippets.

@davbeck
Created January 12, 2012 02:14
Show Gist options
  • Save davbeck/1598129 to your computer and use it in GitHub Desktop.
Save davbeck/1598129 to your computer and use it in GitHub Desktop.
A category to draw an NSImage using 9 slice stretching
//
// NSImage+TMStretchable.h
// ThinkMessenger
//
// Created by David Beck on 1/11/12.
// Copyright (c) 2012 ThinkUltimate. All rights reserved.
//
#import <AppKit/AppKit.h>
typedef struct {
CGFloat top, left, bottom, right;
} TMEdgeInsets;
#define UIEdgeInsetsZero { 0.0, 0.0, 0.0, 0.0 }
TMEdgeInsets TMEdgeInsetsMake (CGFloat top,
CGFloat left,
CGFloat bottom,
CGFloat right);
@interface NSImage (TMStretchable)
- (void)drawInRect:(CGRect)rect withCapInsets:(TMEdgeInsets)capInsets;
@end
//
// NSImage+TMStretchable.m
// ThinkMessenger
//
// Created by David Beck on 1/11/12.
// Copyright (c) 2012 ThinkUltimate. All rights reserved.
//
#import "NSImage+TMStretchable.h"
TMEdgeInsets TMEdgeInsetsMake (CGFloat top,
CGFloat left,
CGFloat bottom,
CGFloat right)
{
TMEdgeInsets insets;
insets.top = top;
insets.left = left;
insets.bottom = bottom;
insets.right = right;
return insets;
}
@implementation NSImage (TMStretchable)
- (void)drawInRect:(CGRect)rect withCapInsets:(TMEdgeInsets)capInsets
{
rect.origin.x = round(rect.origin.x);
rect.origin.y = round(rect.origin.y);
rect.size.width = round(rect.size.width);
rect.size.height = round(rect.size.height);
//bottom left
[self drawInRect:NSMakeRect(rect.origin.x, rect.origin.y,
capInsets.left, capInsets.bottom)
fromRect:NSMakeRect(0.0, 0.0,
capInsets.left, capInsets.bottom)
operation:NSCompositeSourceOver fraction:1.0];
//top left
[self drawInRect:NSMakeRect(rect.origin.x, rect.origin.y + rect.size.height - capInsets.top,
capInsets.left, capInsets.top)
fromRect:NSMakeRect(0.0, self.size.height - capInsets.top,
capInsets.left, capInsets.top)
operation:NSCompositeSourceOver fraction:1.0];
//top right
[self drawInRect:NSMakeRect(rect.origin.x + rect.size.width - capInsets.right, rect.origin.y + rect.size.height - capInsets.top,
capInsets.right, capInsets.top)
fromRect:NSMakeRect(self.size.width - capInsets.right, self.size.height - capInsets.top,
capInsets.right, capInsets.top)
operation:NSCompositeSourceOver fraction:1.0];
//bottom right
[self drawInRect:NSMakeRect(rect.origin.x + rect.size.width - capInsets.right, rect.origin.y,
capInsets.right, capInsets.bottom)
fromRect:NSMakeRect(self.size.width - capInsets.right, 0.0,
capInsets.right, capInsets.bottom)
operation:NSCompositeSourceOver fraction:1.0];
//bottom center
[self drawInRect:NSMakeRect(rect.origin.x + capInsets.left, rect.origin.y,
rect.size.width - capInsets.right - capInsets.left, capInsets.bottom)
fromRect:NSMakeRect(capInsets.left, 0.0,
self.size.width - capInsets.right - capInsets.left, capInsets.bottom)
operation:NSCompositeSourceOver fraction:1.0];
//top center
[self drawInRect:NSMakeRect(rect.origin.x + capInsets.left, rect.origin.y + rect.size.height - capInsets.top,
rect.size.width - capInsets.right - capInsets.left, capInsets.top)
fromRect:NSMakeRect(capInsets.left, self.size.height - capInsets.top,
self.size.width - capInsets.right - capInsets.left, capInsets.top)
operation:NSCompositeSourceOver fraction:1.0];
//left center
[self drawInRect:NSMakeRect(rect.origin.x, rect.origin.y + capInsets.bottom,
capInsets.left, rect.size.height - capInsets.top - capInsets.bottom)
fromRect:NSMakeRect(0.0, capInsets.bottom,
capInsets.left, self.size.height - capInsets.top - capInsets.bottom)
operation:NSCompositeSourceOver fraction:1.0];
//right center
[self drawInRect:NSMakeRect(rect.origin.x + rect.size.width - capInsets.right, rect.origin.y + capInsets.bottom,
capInsets.right, rect.size.height - capInsets.top - capInsets.bottom)
fromRect:NSMakeRect(self.size.width - capInsets.right, capInsets.bottom,
capInsets.right, self.size.height - capInsets.top - capInsets.bottom)
operation:NSCompositeSourceOver fraction:1.0];
//center center
[self drawInRect:NSMakeRect(rect.origin.x + capInsets.left, rect.origin.y + capInsets.bottom,
rect.size.width - capInsets.right - capInsets.left, rect.size.height - capInsets.top - capInsets.bottom)
fromRect:NSMakeRect(capInsets.left, capInsets.bottom,
self.size.width - capInsets.right - capInsets.left, self.size.height - capInsets.top - capInsets.bottom)
operation:NSCompositeSourceOver fraction:1.0];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment