UI视图的圆角设置

使用cornerRadius进行圆角设置

UILabel *labelLayer = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 220, 80)];
[self.view addSubview:labelLayer];
labelLayer.backgroundColor = UIColor.greenColor;
labelLayer.text = @"layer设置圆角";
labelLayer.textColor = UIColor.blackColor;
labelLayer.textAlignment = NSTextAlignmentCenter;
// 设置圆角
labelLayer.layer.cornerRadius = 20;
labelLayer.layer.masksToBounds = YES;

UI视图的圆角设置_第1张图片

iOS11中设置圆角时,同样可以使用cornerRadius进行设置,但是又可以使用属性maskedCorners设置指定位置的圆角。

UILabel *labelLayer2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 120, 220, 80)];
[self.view addSubview:labelLayer2];
labelLayer2.backgroundColor = UIColor.yellowColor;
labelLayer2.text = @"iOS11 layer设置圆角";
labelLayer2.textColor = UIColor.blackColor;
labelLayer2.textAlignment = NSTextAlignmentCenter;
// 设置圆角
labelLayer2.layer.cornerRadius = 20;
labelLayer2.layer.masksToBounds = YES;
if (@available(iOS 11.0, *)) {
        labelLayer2.layer.maskedCorners = (kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner);
}

UI视图的圆角设置_第2张图片
使用贝塞尔曲线UIBezierPath在指定位置设置圆角

UILabel *labelBezier = [[UILabel alloc] initWithFrame:CGRectMake(20, 220, 220, 120)];
[self.view addSubview:labelBezier];
labelBezier.backgroundColor = UIColor.orangeColor;
labelBezier.text = @"UIBezierPath设置圆角";
labelBezier.textColor = UIColor.blackColor;
labelBezier.textAlignment = NSTextAlignmentCenter;
// 指定位置设置圆角
CGFloat radius = 20;
UIRectCorner corner = (UIRectCornerTopRight | UIRectCornerBottomRight);
UIBezierPath *cornerPath = [UIBezierPath bezierPathWithRoundedRect:labelBezier.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *cornerShape = [[CAShapeLayer alloc] init];
cornerShape.frame = labelBezier.bounds;
cornerShape.path = cornerPath.CGPath;
labelBezier.layer.mask = cornerShape;

UI视图的圆角设置_第3张图片

你可能感兴趣的:(iOS,开发编码收集)