以UILabel举例,效果图如下:
方法一仅支持iOS11以上
方法一:
[_sleepStateLabel.layer setMasksToBounds:YES];
[_sleepStateLabel.layer setCornerRadius:12];
[_sleepStateLabel.layer setMaskedCorners:kCALayerMinXMinYCorner | kCALayerMaxXMaxYCorner];
方法二:
//设置左上与右下圆角(可自行编辑指定圆角位置)
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_sleepStateLabel.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(12, 12)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = _sleepStateLabel.bounds;
maskLayer.path = maskPath.CGPath;
_sleepStateLabel.layer.mask = maskLayer;