How to draw rounded images (like application icons) for iPhone.

The normal way is to create a rounded NSBezierPath, set the clipping, and then do your drawing.

I believe rounded rect NSBezierPath was added in 10.5. I use the following category code for 10.4.

NSBezierPath* myRoundedIconRect = [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(itemRect,-0.5,-0.5) cornerRadius:kIAIconRadius];
  [myRoundedIconRect fill];


or

  [myRoundedRect addClip];
  // do whatever drawing you want

Don't forget to save and restore the NSGraphicsContext

CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
  CGContextSaveGState( context );

and

CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
  CGContextRestoreGState( context );

Sorry about the code formatting.

@implementation NSBezierPath (RoundedRect)

+ (NSBezierPath *)bezierPathWithRoundedRect:(NSRect)rect cornerRadius:(float)radius {
    NSBezierPath *result = [NSBezierPath bezierPath];
    [result appendBezierPathWithRoundedRect:rect cornerRadius:radius];
    return result;
}

- (void)appendBezierPathWithRoundedRect:(NSRect)rect cornerRadius:(float)radius {
    if (!NSIsEmptyRect(rect)) {
  if (radius > 0.0) {
      // Clamp radius to be no larger than half the rect's width or height.
float clampedRadius = MIN(radius, 0.5 * MIN(rect.size.width, rect.size.height));

      NSPoint topLeft = NSMakePoint(NSMinX(rect), NSMaxY(rect));
      NSPoint topRight = NSMakePoint(NSMaxX(rect), NSMaxY(rect));
      NSPoint bottomRight = NSMakePoint(NSMaxX(rect), NSMinY(rect));

      [self moveToPoint:NSMakePoint(NSMidX(rect), NSMaxY(rect))];
[self appendBezierPathWithArcFromPoint:topLeft toPoint:rect.origin radius:clampedRadius]; [self appendBezierPathWithArcFromPoint:rect.origin toPoint:bottomRight radius:clampedRadius]; [self appendBezierPathWithArcFromPoint:bottomRight toPoint:topRight radius:clampedRadius]; [self appendBezierPathWithArcFromPoint:topRight toPoint:topLeft radius:clampedRadius];
      [self closePath];
  } else {
// When radius == 0.0, this degenerates to the simple case of a plain rectangle.
      [self appendBezierPathWithRect:rect];
  }
    }
}

@end

Enjoy,
   Peter.

--
              Keyboard Maestro 3 Now Available!
                Now With Status Menu triggers!

Keyboard Maestro <http://www.keyboardmaestro.com/> Macros for your Mac
<http://www.stairways.com/>           <http://download.stairways.com/>
_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to