Re: How to draw rounded Images
Re: How to draw rounded Images
- Subject: Re: How to draw rounded Images
- From: Peter N Lewis <email@hidden>
- Date: Thu, 4 Dec 2008 15:34:00 +0900
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@hidden)
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:
This email sent to email@hidden