Drawing rounded rect in core graphics

此生再无相见时 提交于 2019-11-28 11:39:23

Your problem is that the stroke is applied on the center of the path and half of it gets cropped/masked to the bounds of your view since it draws outside of your view. If you inset your drawing one point in every direction you will have the result you are looking for. If you increase the width of the stroke you will need to inset the drawing further (by half the width of the stroke (i.e a 4 points wide stroke should be inset 2 points).

This can easily be fixed by changing

CGRect rrect = self.bounds;

into

// Inset x and y by half the stroke width (1 point for 2 point stroke) 
CGRect rrect = CGRectInset(self.bounds, 1, 1);

It's because a stroke is centered on the path, which means half of it is inside your rectangle and the other half is outside.

To solve this, double your stroke width and clip to the path before stroking. The result is an inner stroke, which will be entirely within the rectangle.

Note that clipping to the current path in Quartz will reset the current path, so you should create the path as a CGPath so that you can add it, clip to it, add it again, and stroke it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!