/ Published in: Objective C
Drawing scaled rectangles of arbitrary width and height that keeps the corner and sides properly scaled while growing the center.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
+ (void)initialize; { if (baseImage) return; NSRect tileRect = NSMakeRect(0,0,8,8); [topLeftCornerImage lockFocus]; [baseImage drawInRect:tileRect fromRect:NSMakeRect(0.0,16.0,8.0,8.0) operation:NSCompositeCopy fraction:1.0]; [topLeftCornerImage unlockFocus]; [topEdgeImage lockFocus]; [baseImage drawInRect:tileRect fromRect:NSMakeRect(8.0,16.0,8.0,8.0) operation:NSCompositeCopy fraction:1.0]; [topEdgeImage unlockFocus]; [topRightCornerImage lockFocus]; [baseImage drawInRect:tileRect fromRect:NSMakeRect(16.0,16.0,8.0,8.0) operation:NSCompositeCopy fraction:1.0]; [topRightCornerImage unlockFocus]; [leftEdgeImage lockFocus]; [baseImage drawInRect:tileRect fromRect:NSMakeRect(0,8.0,8.0,8.0) operation:NSCompositeCopy fraction:1.0]; [leftEdgeImage unlockFocus]; [centerImage lockFocus]; [baseImage drawInRect:tileRect fromRect:NSMakeRect(8.0,8.0,8.0,8.0) operation:NSCompositeCopy fraction:1.0]; [centerImage unlockFocus]; [rightEdgeImage lockFocus]; [baseImage drawInRect:tileRect fromRect:NSMakeRect(16.0,8.0,8.0,8.0) operation:NSCompositeCopy fraction:1.0]; [rightEdgeImage unlockFocus]; [bottomLeftCornerImage lockFocus]; [baseImage drawInRect:tileRect fromRect:NSMakeRect(0,0,8.0,8.0) operation:NSCompositeCopy fraction:1.0]; [bottomLeftCornerImage unlockFocus]; [bottomEdgeImage lockFocus]; [baseImage drawInRect:tileRect fromRect:NSMakeRect(8.0,0,8.0,8.0) operation:NSCompositeCopy fraction:1.0]; [bottomEdgeImage unlockFocus]; [bottomRightCornerImage lockFocus]; [baseImage drawInRect:tileRect fromRect:NSMakeRect(16.0,0,8.0,8.0) operation:NSCompositeCopy fraction:1.0]; [bottomRightCornerImage unlockFocus]; } - (void)drawRect:(NSRect)rect; { NSDrawNinePartImage([self bounds], topLeftCornerImage, topEdgeImage, topRightCornerImage, leftEdgeImage, centerImage, rightEdgeImage, bottomLeftCornerImage, bottomEdgeImage, bottomRightCornerImage, NSCompositeSourceOver, 1.0, NO); }
URL: http://www.karlkraft.com/index.php/2007/11/14/nsdrawninepartimage/