Return to Snippet

Revision: 30726
at August 20, 2010 08:22 by zingo


Initial Code
static NSImage *topLeftCornerImage;
static NSImage *topEdgeImage;
static NSImage *topRightCornerImage;
static NSImage *leftEdgeImage;
static NSImage *centerImage;
static NSImage *rightEdgeImage;
static NSImage *bottomLeftCornerImage;
static NSImage *bottomEdgeImage;
static NSImage *bottomRightCornerImage;

+ (void)initialize;
{
  if (baseImage) return;

  NSRect tileRect = NSMakeRect(0,0,8,8);

  baseImage = [NSImage imageNamed:@"SoundPickerBackground"];

  topLeftCornerImage = [[NSImage alloc] initWithSize:tileRect.size];
  [topLeftCornerImage lockFocus];
  [baseImage drawInRect:tileRect
               fromRect:NSMakeRect(0.0,16.0,8.0,8.0)
              operation:NSCompositeCopy fraction:1.0];
  [topLeftCornerImage unlockFocus];

  topEdgeImage = [[NSImage alloc] initWithSize:tileRect.size];
  [topEdgeImage lockFocus];
  [baseImage drawInRect:tileRect
               fromRect:NSMakeRect(8.0,16.0,8.0,8.0)
              operation:NSCompositeCopy fraction:1.0];
  [topEdgeImage unlockFocus];

  topRightCornerImage = [[NSImage alloc] initWithSize:tileRect.size];
  [topRightCornerImage lockFocus];
  [baseImage drawInRect:tileRect
               fromRect:NSMakeRect(16.0,16.0,8.0,8.0)
              operation:NSCompositeCopy fraction:1.0];
  [topRightCornerImage unlockFocus];

  leftEdgeImage = [[NSImage alloc] initWithSize:tileRect.size];
  [leftEdgeImage lockFocus];
  [baseImage drawInRect:tileRect
               fromRect:NSMakeRect(0,8.0,8.0,8.0)
              operation:NSCompositeCopy fraction:1.0];
  [leftEdgeImage unlockFocus];

  centerImage = [[NSImage alloc] initWithSize:tileRect.size];
  [centerImage lockFocus];
  [baseImage drawInRect:tileRect
               fromRect:NSMakeRect(8.0,8.0,8.0,8.0)
              operation:NSCompositeCopy fraction:1.0];
  [centerImage unlockFocus];

  rightEdgeImage = [[NSImage alloc] initWithSize:tileRect.size];
  [rightEdgeImage lockFocus];
  [baseImage drawInRect:tileRect
               fromRect:NSMakeRect(16.0,8.0,8.0,8.0)
              operation:NSCompositeCopy fraction:1.0];
  [rightEdgeImage unlockFocus];

  bottomLeftCornerImage = [[NSImage alloc] initWithSize:tileRect.size];
  [bottomLeftCornerImage lockFocus];
  [baseImage drawInRect:tileRect
               fromRect:NSMakeRect(0,0,8.0,8.0)
              operation:NSCompositeCopy fraction:1.0];
  [bottomLeftCornerImage unlockFocus];

  bottomEdgeImage = [[NSImage alloc] initWithSize:tileRect.size];
  [bottomEdgeImage lockFocus];
  [baseImage drawInRect:tileRect
               fromRect:NSMakeRect(8.0,0,8.0,8.0)
              operation:NSCompositeCopy fraction:1.0];
  [bottomEdgeImage unlockFocus];

  bottomRightCornerImage = [[NSImage alloc] initWithSize:tileRect.size];
  [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);
}

Initial URL
http://www.karlkraft.com/index.php/2007/11/14/nsdrawninepartimage/

Initial Description
Drawing scaled rectangles of arbitrary width and height that keeps the corner and sides properly scaled while growing the center.

Initial Title
Drawing Scaled Rectangles with NSDrawNinePartImage

Initial Tags
osx

Initial Language
Objective C