CocoaDev

Edit AllPages

A category for NSBezierPath which allows for very simple drawing of rectangles with arced corners – like the bezel service popups.

“Ouch. Didn’t notice this page until I added NSBezierPathCategory. Sorry.”

The interface & implementation:

@interface NSBezierPath(RoundedRectangle)

/** Returns a closed bezier path describing a rectangle with curved corners The corner radius will be trimmed to not exceed half of the lesser rectangle dimension. */

@end

@implementation NSBezierPath(RoundedRectangle)

@end

I hope somebody will find this useful. See the snapshot below:

http://home.earthlink.net/~zakariya/files/RoundedRects.gif

This demonstrates a neat feature: notice that in the first shot, it’s a circle, in the second it’s a lozenge, and the third, it’s a rectangle with rounded corners – this is because it trims the corner radius to best fit the rectangle size. Cool, eh?

–ShamylZakariya


Maybe it could be made to use NSAffineTransform instead of the 20 or so adds. Or alternatively, store the points in an array and use a loop.


Optimization concerns aside, awesome. I love a good sample.

– RobRix

I just read up on the affine transformation – it makes sense, so I think I’ll test it and should it work, I’ll update the example code. And RobRix, thanks for the support!

UPDATE – I changed the code to use an NSAffineTransform

–ShamylZakariya


You might be able to simplify it further by a) getting rid of the lineToPoint: (which are implicit when you add a new arc) and b) by using NSInsetRect rather than calculate each end corner, i.e. the code I have here goes something like:


You win ;) – ShamylZakariya


Speaking of roundRects… Is there a simple way to display selection RoundRects around items in a text field, like Mail.app does when hovering the mouse around e-mail addresses? (The small triangle with the pop-up menu would also be neat).

Or is this advanced stuff?

Thanks,

– Andr�s

Here is some gal’s implementation of this feature: http://www.harmless.de/cocoa.html#rollover Actually, the author of this site and its referenced projects is male … look up the name’s etymology. :-)

** In Tiger, you now have NSTokenField for this.**


Any tips for getting this in a tableview like acquisition, cssedit etc.?

Thanks :)

See RoundedRectRowHighlighting


Leopard adds + bezierPathWithRoundedRect:xRadius:yRadius: and � appendBezierPathWithRoundedRect:xRadius:yRadius:.