CGContext Line Width

I am trying to make a simple line, the problem is that it doesn’t come out as 1 pixel wide, but 2. Documents state that user space units will translate to pixel if I read it correctly.

The code is as simple as it is, but my line is always 2 pixels wide.

//Get the CGContext from this view CGContextRef context = UIGraphicsGetCurrentContext(); //Set the stroke (pen) color CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); //Set the width of the pen mark CGContextSetLineWidth(context, 1); for (int i = 1; i <= sections - 1; i++) { UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:CGPointMake(0.0, i * kSectionHeight)]; CGContextMoveToPoint(context, 0.0, i * kSectionHeight); //Start point CGContextAddLineToPoint(context, self.frame.size.width, i * kSectionHeight); } CGContextStrokePath(context); 

enter image description here

+4
source share
2 answers

No dots translate to pixels. If your line is too thick, change the value.

+3
source

In a test application, I tried the following:

 CGFloat desiredWidthInPixels = 1.0f; CGFloat lineWidth = desiredWidthInPixels / [[UIScreen mainScreen] scale]; NSLog(@"lineWidth = %f", lineWidth); 

I got a line of width 0.5f, which should exactly translate to one pixel width on the screen (since the scale is 2.0, which indicates a Retina device). This should properly adapt to devices of different screen sizes.

*** Caution: Of course, this only works as long as the [[UIScreen mainScreen] scale] works as it is now.

0
source

Source: https://habr.com/ru/post/1499047/


All Articles