Re: NSBezierPath geometry question...
Re: NSBezierPath geometry question...
- Subject: Re: NSBezierPath geometry question...
- From: Serge Meynard <email@hidden>
- Date: Sun, 10 Apr 2005 18:37:25 -0400
On Apr 10, 2005, at 17:40, Keith Blount wrote:
Hello,
I am trying to use NSBezierPath to create a shape that
highlights text in a text view. Basically, I want to
create a shape that has rounded corners (like the
traditional Apple rounded rectangles you get in Mail
etc), but this may highlight text that spills over
several lines - so the shape is irregular.
This shape is actually defined by several rectangles
that are got using:
NSRectArray rectArray = [layoutManager
rectArrayForCharacterRange:charRange
withinSelectedCharacterRange:NSMakeRange(NSNotFound,0)
inTextContainer:textContainer rectCount:&rectCount];
So what I actually need to do is take the rectangles
returned by this method, create one compound shape
from them, and then round the corners of this shape
into one NSBezierPath.
I have tried several approaches with differing degrees
of succes, such as getting all of the right points
then all of the left points and using
appendBezierPathWithArcFromPoint..., for instance, but
everything I have tried has had some odd effects.
Does anybody know how to take several rects and turn
them into one shape with rounded corners?
Many thanks for any advice,
Keith
Well, the first issue I can see here is whether or not the rectangles
all touch or not. If there are gaps between them, then you'll have to
adjust the rects first to eliminate those gaps.
Since the rectangles represent ranges of text, we can assume they are
all vertically stacked and non-overlapping, which simplifies things.
Assuming also that you have them ordered from top to bottom, you could
start at the top left corner of the top rectangle and go down:
rect1.topleft, rect1.bottomLeft, rect2.topLeft, rect2.bottomLeft,
rect3.topLeft, etc. When you get to the bottom rect, move over to the
right side and go back up.
As you go, generate segments in your bezier that match each pair of
edges. For example, the first segment would correspond to the two edges
formed by the three points (rect1.topLeft, rect1.bottomLeft,
rect2.topLeft). For a given set of three points, the rounded corner can
be created using curveToPoint:controlPoint1:controlPoint2: where both
control points are the middle point of the set (the corner). The
endpoints of the curveTo call you'll have to calculate depending on the
amount of curvature you want; interpolate them somewhere between
point1/point2 and point2/point3 respectively, with a ratio anywhere
from 0.0 to 0.5. If your endpoints are not at 0.5, you'll have to
create straight line segments between the end of one curve and the
start of the next one.
I hope all this isn't too confused! :)
Serge
_______________________________________________
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