调用方法时需要设置哪个边角就穿哪个角,很容易控制的。
/**
设置圆角
@param view 需要设置圆角的控件
@param corners 哪个圆角
@param radii 圆角size
@return layer图层
*/
-(CALayer *)configRectCorner:(UIView *)view corners:(UIRectCorner)corners radii:(CGSize)radii{
/*
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
};
~0UL 相当于 -1
*/
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:radii];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = view.bounds;
maskLayer.path = maskPath.CGPath;
return maskLayer;
}
调用
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 100, 50)];
label.backgroundColor = [UIColor orangeColor];
label.textColor = [UIColor whiteColor];
label.text = @"圆角设置";
label.textAlignment = NSTextAlignmentCenter;
label.layer.mask = [self configRectCorner:label corners: UIRectCornerTopRight | UIRectCornerBottomRight radii:CGSizeMake(15, 15)];
[self.view addSubview:label];
网友评论