Created
January 16, 2012 15:23
-
-
Save eaigner/1621353 to your computer and use it in GitHub Desktop.
Stretchable NSImage
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
@implementation NSImage (Resizable) | |
- (void)drawStretchableInRect:(NSRect)rect edgeInsets:(NSEdgeInsets)insets operation:(NSCompositingOperation)op fraction:(CGFloat)delta { | |
NSRect srcRect = (NSRect){NSZeroPoint, self.size}; | |
NSRect topLeft = NSMakeRect(NSMinX(srcRect), NSMaxY(srcRect) - insets.top, insets.left, insets.top); | |
NSRect centerLeft = NSMakeRect(NSMinX(srcRect), NSMinY(srcRect) + insets.bottom, insets.left, NSHeight(srcRect) - insets.top - insets.bottom); | |
NSRect bottomLeft = NSMakeRect(NSMinX(srcRect), NSMinY(srcRect), insets.left, insets.bottom); | |
NSRect topCenter = NSMakeRect(NSMaxX(topLeft), | |
NSMaxY(srcRect) - insets.top, | |
NSWidth(srcRect) - insets.left - insets.right, | |
insets.top); | |
NSRect centerCenter = NSMakeRect(NSMaxX(centerLeft), | |
NSMinY(srcRect) + insets.bottom, | |
NSWidth(srcRect) - insets.left - insets.right, | |
NSHeight(srcRect) - insets.top - insets.bottom); | |
NSRect bottomCenter = NSMakeRect(NSMaxX(bottomLeft), | |
NSMinY(srcRect), | |
NSWidth(srcRect) - insets.left - insets.right, | |
insets.bottom); | |
NSRect topRight = NSMakeRect(NSMaxX(srcRect) - insets.right, | |
NSMaxY(srcRect) - insets.top, | |
insets.right, | |
insets.top); | |
NSRect centerRight = NSMakeRect(NSMaxX(srcRect) - insets.right, | |
NSMinY(srcRect) + insets.bottom, | |
insets.right, | |
NSHeight(srcRect) - insets.top - insets.bottom); | |
NSRect bottomRight = NSMakeRect(NSMaxX(srcRect) - insets.right, | |
NSMinY(srcRect), | |
insets.right, | |
insets.bottom); | |
[self drawInRect:NSMakeRect(NSMinX(rect), NSMaxY(rect) - insets.top, insets.left, insets.top) | |
fromRect:topLeft operation:op fraction:delta]; | |
[self drawInRect:NSMakeRect(NSMinX(rect), NSMinY(rect) + insets.bottom, insets.left, NSHeight(rect) - insets.top - insets.bottom) | |
fromRect:centerLeft operation:op fraction:delta]; | |
[self drawInRect:NSMakeRect(NSMinX(rect), NSMinY(rect), insets.left, insets.bottom) | |
fromRect:bottomLeft operation:op fraction:delta]; | |
[self drawInRect:NSMakeRect(NSMinX(rect) + insets.left, NSMaxY(rect) - insets.top, NSWidth(rect) - insets.left - insets.right, insets.top) | |
fromRect:topCenter operation:op fraction:delta]; | |
[self drawInRect:NSMakeRect(NSMinX(rect) + insets.left, NSMinY(rect) + insets.bottom, NSWidth(rect) - insets.left - insets.right, NSHeight(rect) - insets.top - insets.bottom) fromRect:centerCenter operation:op fraction:delta]; | |
[self drawInRect:NSMakeRect(NSMinX(rect) + insets.left, NSMinY(rect), NSWidth(rect) - insets.left - insets.right, insets.bottom) | |
fromRect:bottomCenter operation:op fraction:delta]; | |
[self drawInRect:NSMakeRect(NSMaxX(rect) - insets.right, NSMaxY(rect) - insets.top, insets.right, insets.top) | |
fromRect:topRight operation:op fraction:delta]; | |
[self drawInRect:NSMakeRect(NSMaxX(rect) - insets.right, NSMinY(rect) + insets.bottom, insets.right, NSHeight(rect) - insets.top - insets.bottom) | |
fromRect:centerRight operation:op fraction:delta]; | |
[self drawInRect:NSMakeRect(NSMaxX(rect) - insets.right, NSMinY(rect), insets.right, insets.bottom) | |
fromRect:bottomRight operation:op fraction:delta]; | |
} | |
@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 characters
@implementation NSImage (Resizable) | |
- (void)drawStretchableInRect:(NSRect)rect edgeInsets:(NSEdgeInsets)insets operation:(NSCompositingOperation)op fraction:(CGFloat)delta { | |
void (^makeAreas)(NSRect, NSRect *, NSRect *, NSRect *, NSRect *, NSRect *, NSRect *, NSRect *, NSRect *, NSRect *) = ^(NSRect srcRect, NSRect *tl, NSRect *tc, NSRect *tr, NSRect *ml, NSRect *mc, NSRect *mr, NSRect *bl, NSRect *bc, NSRect *br) { | |
CGFloat w = NSWidth(srcRect); | |
CGFloat h = NSHeight(srcRect); | |
CGFloat cw = w - insets.left - insets.right; | |
CGFloat ch = h - insets.top - insets.bottom; | |
CGFloat x0 = NSMinX(srcRect); | |
CGFloat x1 = x0 + insets.left; | |
CGFloat x2 = NSMaxX(srcRect) - insets.right; | |
CGFloat y0 = NSMinY(srcRect); | |
CGFloat y1 = y0 + insets.bottom; | |
CGFloat y2 = NSMaxY(srcRect) - insets.top; | |
*tl = NSMakeRect(x0, y2, insets.left, insets.top); | |
*tc = NSMakeRect(x1, y2, cw, insets.top); | |
*tr = NSMakeRect(x2, y2, insets.right, insets.top); | |
*ml = NSMakeRect(x0, y1, insets.left, ch); | |
*mc = NSMakeRect(x1, y1, cw, ch); | |
*mr = NSMakeRect(x2, y1, insets.right, ch); | |
*bl = NSMakeRect(x0, y0, insets.left, insets.bottom); | |
*bc = NSMakeRect(x1, y0, cw, insets.bottom); | |
*br = NSMakeRect(x2, y0, insets.right, insets.bottom); | |
}; | |
// Source rects | |
NSRect srcRect = (NSRect){NSZeroPoint, self.size}; | |
NSRect srcTopL, srcTopC, srcTopR, srcMidL, srcMidC, srcMidR, srcBotL, srcBotC, srcBotR; | |
makeAreas(srcRect, &srcTopL, &srcTopC, &srcTopR, &srcMidL, &srcMidC, &srcMidR, &srcBotL, &srcBotC, &srcBotR); | |
// Destinations rects | |
NSRect dstTopL, dstTopC, dstTopR, dstMidL, dstMidC, dstMidR, dstBotL, dstBotC, dstBotR; | |
makeAreas(rect, &dstTopL, &dstTopC, &dstTopR, &dstMidL, &dstMidC, &dstMidR, &dstBotL, &dstBotC, &dstBotR); | |
// Draw | |
[self drawInRect:dstTopL fromRect:srcTopL operation:op fraction:delta]; | |
[self drawInRect:dstTopC fromRect:srcTopC operation:op fraction:delta]; | |
[self drawInRect:dstTopR fromRect:srcTopR operation:op fraction:delta]; | |
[self drawInRect:dstMidL fromRect:srcMidL operation:op fraction:delta]; | |
[self drawInRect:dstMidC fromRect:srcMidC operation:op fraction:delta]; | |
[self drawInRect:dstMidR fromRect:srcMidR operation:op fraction:delta]; | |
[self drawInRect:dstBotL fromRect:srcBotL operation:op fraction:delta]; | |
[self drawInRect:dstBotC fromRect:srcBotC operation:op fraction:delta]; | |
[self drawInRect:dstBotR fromRect:srcBotR operation:op fraction:delta]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment