angraankit
angraankit

Reputation: 125

Uneven thickness in UIBezierPath

I am experimenting with UIBezierPath. I want to make a grid view.

NSInteger yAxisIncremental = 0;
NSInteger xAxisIncremental = 0;

UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 1;

for (int x = 0; x <5; x++) {
    [path moveToPoint:CGPointMake(xAxisIncremental, 0)];
    [path addLineToPoint:CGPointMake(xAxisIncremental, self.frame.size.height)];
    xAxisIncremental = xAxisIncremental + kWidthOfSquare;
}

for (int x = 0; x <=5; x++) {
    [path moveToPoint:CGPointMake(0, yAxisIncremental)];
    [path addLineToPoint:CGPointMake(self.frame.size.width, yAxisIncremental)];
    yAxisIncremental = yAxisIncremental + kHeightOfSquare;
}

[[UIColor blackColor] setStroke];
[path stroke];

enter image description here

Notice the first line is thin and corresponding lines are a little thick.Have I done something wrong in the code or is it the expected behavior?

Upvotes: 1

Views: 878

Answers (2)

Koen
Koen

Reputation: 59

Here is some sample code for a rect points generation with safe area for the stroke.

objective-c

  UIBezierPath *aPath = [UIBezierPath bezierPath];
    CGFloat safeArea  = 1.5;
    CGFloat minX = rect.origin.x+safeArea;
    CGFloat minY = rect.origin.y+safeArea;
    CGFloat maxX = rect.origin.x+rect.size.width-safeArea;
    CGFloat maxY = rect.origin.y+rect.size.height-safeArea;
    //draw your rectangle with stroke safe area*2
...

.

Upvotes: 0

Palle
Palle

Reputation: 12109

The first line you are drawing begins at 0. This means that half of the width of the line will probably be offscreen or outside of your view. You can solve this by starting at pixel 0.5 instead of 0.

Also if you choose to have a line width of 1.0 this means that on high-dpi devices the lines will be thicker than 1 actual pixel on the screen. To draw a line which is 1px in width you can use a lineWidth of 0 as stated in the UIBezierPath documentation:

A width of 0 is interpreted as the thinnest line that can be rendered on a particular device.

Upvotes: 2

Related Questions