Drawing Scaled Rectangles with NSDrawNinePartImage


/ Published in: Objective C
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. static NSImage *topLeftCornerImage;
  2. static NSImage *topEdgeImage;
  3. static NSImage *topRightCornerImage;
  4. static NSImage *leftEdgeImage;
  5. static NSImage *centerImage;
  6. static NSImage *rightEdgeImage;
  7. static NSImage *bottomLeftCornerImage;
  8. static NSImage *bottomEdgeImage;
  9. static NSImage *bottomRightCornerImage;
  10.  
  11. + (void)initialize;
  12. {
  13. if (baseImage) return;
  14.  
  15. NSRect tileRect = NSMakeRect(0,0,8,8);
  16.  
  17. baseImage = [NSImage imageNamed:@"SoundPickerBackground"];
  18.  
  19. topLeftCornerImage = [[NSImage alloc] initWithSize:tileRect.size];
  20. [topLeftCornerImage lockFocus];
  21. [baseImage drawInRect:tileRect
  22. fromRect:NSMakeRect(0.0,16.0,8.0,8.0)
  23. operation:NSCompositeCopy fraction:1.0];
  24. [topLeftCornerImage unlockFocus];
  25.  
  26. topEdgeImage = [[NSImage alloc] initWithSize:tileRect.size];
  27. [topEdgeImage lockFocus];
  28. [baseImage drawInRect:tileRect
  29. fromRect:NSMakeRect(8.0,16.0,8.0,8.0)
  30. operation:NSCompositeCopy fraction:1.0];
  31. [topEdgeImage unlockFocus];
  32.  
  33. topRightCornerImage = [[NSImage alloc] initWithSize:tileRect.size];
  34. [topRightCornerImage lockFocus];
  35. [baseImage drawInRect:tileRect
  36. fromRect:NSMakeRect(16.0,16.0,8.0,8.0)
  37. operation:NSCompositeCopy fraction:1.0];
  38. [topRightCornerImage unlockFocus];
  39.  
  40. leftEdgeImage = [[NSImage alloc] initWithSize:tileRect.size];
  41. [leftEdgeImage lockFocus];
  42. [baseImage drawInRect:tileRect
  43. fromRect:NSMakeRect(0,8.0,8.0,8.0)
  44. operation:NSCompositeCopy fraction:1.0];
  45. [leftEdgeImage unlockFocus];
  46.  
  47. centerImage = [[NSImage alloc] initWithSize:tileRect.size];
  48. [centerImage lockFocus];
  49. [baseImage drawInRect:tileRect
  50. fromRect:NSMakeRect(8.0,8.0,8.0,8.0)
  51. operation:NSCompositeCopy fraction:1.0];
  52. [centerImage unlockFocus];
  53.  
  54. rightEdgeImage = [[NSImage alloc] initWithSize:tileRect.size];
  55. [rightEdgeImage lockFocus];
  56. [baseImage drawInRect:tileRect
  57. fromRect:NSMakeRect(16.0,8.0,8.0,8.0)
  58. operation:NSCompositeCopy fraction:1.0];
  59. [rightEdgeImage unlockFocus];
  60.  
  61. bottomLeftCornerImage = [[NSImage alloc] initWithSize:tileRect.size];
  62. [bottomLeftCornerImage lockFocus];
  63. [baseImage drawInRect:tileRect
  64. fromRect:NSMakeRect(0,0,8.0,8.0)
  65. operation:NSCompositeCopy fraction:1.0];
  66. [bottomLeftCornerImage unlockFocus];
  67.  
  68. bottomEdgeImage = [[NSImage alloc] initWithSize:tileRect.size];
  69. [bottomEdgeImage lockFocus];
  70. [baseImage drawInRect:tileRect
  71. fromRect:NSMakeRect(8.0,0,8.0,8.0)
  72. operation:NSCompositeCopy fraction:1.0];
  73. [bottomEdgeImage unlockFocus];
  74.  
  75. bottomRightCornerImage = [[NSImage alloc] initWithSize:tileRect.size];
  76. [bottomRightCornerImage lockFocus];
  77. [baseImage drawInRect:tileRect
  78. fromRect:NSMakeRect(16.0,0,8.0,8.0)
  79. operation:NSCompositeCopy fraction:1.0];
  80. [bottomRightCornerImage unlockFocus];
  81. }
  82.  
  83.  
  84.  
  85. - (void)drawRect:(NSRect)rect;
  86. {
  87. NSDrawNinePartImage([self bounds],
  88. topLeftCornerImage, topEdgeImage, topRightCornerImage,
  89. leftEdgeImage, centerImage, rightEdgeImage,
  90. bottomLeftCornerImage, bottomEdgeImage, bottomRightCornerImage,
  91. NSCompositeSourceOver, 1.0, NO);
  92. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.