Re: Round Rect
Re: Round Rect
- Subject: Re: Round Rect
- From: Nicko van Someren <email@hidden>
- Date: Wed, 29 Mar 2006 15:38:18 +0100
On 29 Mar 2006, at 15:06, John Buckley wrote:
You mean a rect with round corners :)
If he didn't then all he needs is +bezierPathWithOvalInRect: :-)
Use an NSBezierPath and construct the points manually.
e.g. - appendBezierPathWithArcFromPoint: toPoint: radius:
can be used to construct the corners.
It's worth noting that this message is (a) not very intuitive and (b)
extremely handy for your task. The docs state:
- (void)appendBezierPathWithArcFromPoint:(NSPoint)fromPoint toPoint:
(NSPoint)toPoint radius:(float)radius
The arc lies on the perimeter of a circle with radius radius
inscribed in an angle defined by the current point, fromPoint, and
toPoint. The arc is drawn between the tangent points of the circle
with the two legs of the angle. The arc usually does not contain
the points fromPoint and toPoint. A line is drawn from the current
point to the starting point of the arc, which is located at the
tangent point of the first leg of the angle.
This is actually rather misleading since fromPoint is basically
_never_ on the resulting path (unless the radius is 0). Since the
fromPoint represents the apex of the angle into which the arc is to
be inscribed it is, in this case, the corner of the rectangle who's
corners you are trying to round. The method also adds the line for
the current point to the start of the arc for you, which is handy.
This thus your code would look something like:
+ (NSBezierPath *) bezierPathWithRoundRectInRect:(NSRect)rect radius:
(float) radius {
NSBezierPath *path = [NSBezierPath bezierPath];
NSPoint bl = NSMakePoint(NSMinX(rect), NSMinY(rect));
NSPoint br = NSMakePoint(NSMaxX(rect), NSMinY(rect));
NSPoint tl = NSMakePoint(NSMinX(rect), NSMaxY(rect));
NSPoint tr = NSMakePoint(NSMaxX(rect), NSMaxY(rect));
[path moveToPoint: NSMakePoint(bl.x + radius, bl.y)];
[path appendBezierPathWithArcFromPoint: br toPoint: tr radius: radius];
[path appendBezierPathWithArcFromPoint: tr toPoint: tl radius: radius];
[path appendBezierPathWithArcFromPoint: tl toPoint: bl radius: radius];
[path appendBezierPathWithArcFromPoint: bl toPoint: br radius: radius];
[path closePath];
return path;
}
Each append... message adds one straight edge and one 90 degree arc.
I would add a category on NSBezierPath to do the work in a single
method.
Indeed. I wrote the code as a class method so it can be used that way.
Cheers,
Nicko
_______________________________________________
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