美文网首页iOS 学习
iOS 分别设置控件四个边角的圆角

iOS 分别设置控件四个边角的圆角

作者: 零粹 | 来源:发表于2019-01-18 10:44 被阅读0次
20190118110315.png

调用方法时需要设置哪个边角就穿哪个角,很容易控制的。

/**
 设置圆角

 @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];

demo https://github.com/ITHanYong/RectCorner.git

相关文章

网友评论

    本文标题:iOS 分别设置控件四个边角的圆角

    本文链接:https://www.haomeiwen.com/subject/fjggdqtx.html