需要借助UIBezierPath與CAShapeLayer進(jìn)行實(shí)現(xiàn)
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(20, 20)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = view.bounds;
maskLayer.path = maskPath.CGPath;
view.layer.mask = maskLayer;
通過枚舉類型UIRectCorner選擇上下左右
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
};
UILabel設(shè)置圓角可能出現(xiàn)問題
如果Uilabel設(shè)置圓角,在分辨率高的手機(jī)可能出現(xiàn)一條細(xì)線,因?yàn)閘abel的寬度不是整數(shù)白对,取整數(shù)即可。