Re: Round Corners With BezierPath
Re: Round Corners With BezierPath
- Subject: Re: Round Corners With BezierPath
- From: Stephen Deken <email@hidden>
- Date: Tue, 22 Aug 2006 22:42:16 -0500
I've experimented with setting the line width to different values
but have had no success. The thought of having to construct the
rectangles with lines and arcs is scary.
It's not as hard as it seems. The best way is probably to add a
category to NSBezierPath. The code below was typed in Mail.app, and
I haven't tested it, but it should work. Use it like so:
NSBezierPath *b = [NSBezierPath bezierPathWithRoundedRect:[self
bounds] radius:10];
[b stroke];
If you're planning on stroking it with the bounds rectangle, you'll
still have to adjust the rectangle to allow the path to be drawn:
NSRect r2 = [self bounds];
r2.size.width -= 2;
r2.size.height -= 2;
r2.origin.x += 1;
r2.origin.y += 1;
[[NSBezierPath bezierPathWithRoundedRect:r2 radius:10] stroke];
--sjd;
@interface NSBezierPath (RoundedRects)
-(void) appendRoundedRect:(NSRect)rect radius:(float)radius;
+(NSBezierPath*) bezierPathWithRoundedRect:(NSRect)rect radius:(float)
radius;
@end
@implementation NSBezierPath (RoundedRects)
-(void) appendRoundedRect:(NSRect)rect radius:(float)radius
{
// if the radius is larger than the rectangle, slim it down
if (radius > rect.size.width / 2.0 || radius > rect.size.height / 2.0)
radius = fmin(rect.size.height,rect.size.width)/2.0;
// this isn't quite right for a circular arc -- you'd have to find
the real value. It's something like 0.556 or so. Google "bezier
path circles".
float radControl = radius * 0.5;
// inner rect is offset by the radius on each side
NSRect innerRect = NSMakeRect(rect.origin.x+radius, rect.origin.y
+radius, rect.size.width - radius*2, rect.size.height-radius*2 );
// starting in the lower left corner
[self moveToPoint:NSMakePoint(innerRect.origin.x-
radius,innerRect.origin.y)];
// left side
[self relativeLineToPoint:NSMakePoint(0,innerRect.size.height)];
// upper left corner
[self relativeCurveToPoint:NSMakePoint(radius,radius)
controlPoint1:NSMakePoint(0,radControl) controlPoint2:NSMakePoint
(radius-radControl,radius)];
// top side
[self relativeLineToPoint:NSMakePoint(innerRect.size.width,0)];
// upper right corner
[self relativeCurveToPoint:NSMakePoint(radius,-radius)
controlPoint1:NSMakePoint(radControl,0) controlPoint2:NSMakePoint
(radius,radControl-radius)];
// right side
[self relativeLineToPoint:NSMakePoint(0,-innerRect.size.height)];
// lower right corner
[self relativeCurveToPoint:NSMakePoint(-radius,-radius)
controlPoint1:NSMakePoint(0,-radControl) controlPoint2:NSMakePoint
(radControl-radius,-radius)];
// bottom side
[self relativeLineToPoint:NSMakePoint(-innerRect.size.width,0)];
// lower left corner
[self relativeCurveToPoint:NSMakePoint(-radius,radius)
controlPoint1:NSMakePoint(-radControl,0) controlPoint2:NSMakePoint(-
radius,radControl)];
}
+(NSBezierPath*) bezierPathWithRoundedRect:(NSRect)rect radius:(float)
radius
{
self = [NSBezierPath bezierPath];
[self appendRoundedRect:rect radius:radius];
return self;
}
@end
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden