How to Draw a single point line in iOS

前端 未结 5 687
北恋
北恋 2020-12-01 19:00

I was wondering what is the best way to draw a single point line? My goal is to draw this line in a tableViewCell to make it look just like the native cell separator. I don\

5条回答
  •  时光取名叫无心
    2020-12-01 19:29

    shapeLayer.lineWidth = 0.5f;
    

    That's a common mistake and is the reason this is working only some of the time. Sometimes this will overlap pixels on the screen exactly and sometimes it won't. The way to draw a single-point line that always works is to draw a one-point-thick rectangle on integer boundaries, and fill it. That way, it will always match the pixels on the screen exactly.

    To convert from points to pixels, if you want to do that, use the view's scale factor.

    Thus, this will always be one pixel wide:

    CGContextFillRect(con, CGRectMake(0,0,desiredLength,1.0/self.contentScaleFactor));
    

    Here's a screen shot showing the line used as a separator, drawn at the top of each cell:

    enter image description here

    The table view itself has no separators (as is shown by the white space below the three existing cells). I may not be drawing the line in the position, length, and color that you want, but that's your concern, not mine.

提交回复
热议问题