Question

Ok, so I'm trying to draw a dashed box that's subdivided into sections where I'm going to put content. Here's my code:

NSBezierPath *path = [NSBezierPath bezierPathWithRect:dirtyRect];
[path setLineWidth:3];
CGFloat pattern[2] = {5, 5};
[path setLineDash:pattern count:2 phase:0];
CGFloat totalHeight = header.frame.origin.y - 10;
CGFloat sectionOffset = 0;
if([game getNumPlayers] == 2) {
    sectionOffset = totalHeight / 2;
} else if([game getNumPlayers] == 3) {
    sectionOffset = totalHeight / 3;
} else if([game getNumPlayers] == 4) {
    sectionOffset = totalHeight / 4;
}
for(int i = 0; i < [[game getPlayers] count]; i++) {
    [path moveToPoint:NSMakePoint(0, totalHeight - (sectionOffset * i))];
    [path lineToPoint:NSMakePoint(dirtyRect.size.width, totalHeight - (sectionOffset * i))];
}
[path stroke];

This is contained within my custom view's drawRect method, so dirtyRect is an NSRect equivalent to the bounds of the view. The variable header refers to another view in the superview, which I'm basing the location of the lines off of.

Here's a screenshot of what this code actually draws (except the label obviously):

As you can see, unless we're dealing with a very unfortunate optically illusion, which I doubt, the dividers contained in the box appear to be thicker than the outline of the box. I've explicitly set the lineWidth of the path object to be three, so I'm not sure why this is. I would be much appreciative of any suggestions that can be provided.

Was it helpful?

Solution

OK, I think the problem is that your outer box is just getting clipped by the edges of its view. You ask for a line that’s 3 points wide, so if your dirtyRect is the actual bounds of the view, then 1.5 points of the enclosing box will be outside the view, so you’ll only see 1.5 points of the edge lines.

The inner lines are showing the full 3-point thickness.

You can fix this by doing something like:

const CGFloat lineWidth = 3;
NSBezierPath *const path = [NSBezierPath bezierPathWithRect:NSInsetRect(dirtyRect, lineWidth/2, lineWidth/2)];
path.lineWidth = lineWidth;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top