UIView设置成圆角
有时候我们需要把图片、textview等view设置成圆角,如下:
头文件:
头文件:
#import <QuartzCore/QuartzCore.h>
code:
view.layer.borderWidth = 1;
view.layer.cornerRadius = 6;
view.layer.masksToBounds = YES;
其他方法可以实现
先把当前View的opaque属性设为NO,然后在drawRect里画个圆角柜形:
CGFloat radius = 20.0;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1);
CGFloat minx = CGRectGetMinX(rect), midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect);
CGFloat miny = CGRectGetMinY(rect), midy = CGRectGetMidY(rect), maxy = CGRectGetMaxY(rect);
CGContextMoveToPoint(context, minx, midy);
CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFill);