// // UIView+Corner.m // YUMI // // Created by YUMI on 2022/6/15. // #import "UIView+Corner.h" @implementation UIView (Corner) - (void)setCornerWithLeftTopCorner:(CGFloat)leftTop rightTopCorner:(CGFloat)rigtTop bottomLeftCorner:(CGFloat)bottemLeft bottomRightCorner:(CGFloat)bottemRight size:(CGSize)size { CGFloat width = size.width; CGFloat height = size.height; UIBezierPath *maskPath = [UIBezierPath bezierPath]; maskPath.lineWidth = 1.0; maskPath.lineCapStyle = kCGLineCapRound; maskPath.lineJoinStyle = kCGLineJoinRound; [maskPath moveToPoint:CGPointMake(bottemRight, height)]; //左下角 [maskPath addLineToPoint:CGPointMake(width - bottemRight, height)]; [maskPath addQuadCurveToPoint:CGPointMake(width, height- bottemRight) controlPoint:CGPointMake(width, height)]; //右下角的圆弧 [maskPath addLineToPoint:CGPointMake(width, rigtTop)]; //右边直线 [maskPath addQuadCurveToPoint:CGPointMake(width - rigtTop, 0) controlPoint:CGPointMake(width, 0)]; //右上角圆弧 [maskPath addLineToPoint:CGPointMake(leftTop, 0)]; //顶部直线 [maskPath addQuadCurveToPoint:CGPointMake(0, leftTop) controlPoint:CGPointMake(0, 0)]; //左上角圆弧 [maskPath addLineToPoint:CGPointMake(0, height - bottemLeft)]; //左边直线 [maskPath addQuadCurveToPoint:CGPointMake(bottemLeft, height) controlPoint:CGPointMake(0, height)]; //左下角圆弧 CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = CGRectMake(0, 0, size.width, size.height); maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; } //- (void)setCornerRadius:(CGFloat)radius // corners:(UIRectCorner)corners // borderWidth:(CGFloat)borderWidth // borderColor:(UIColor *)borderColor { // // // 创建 UIBezierPath 并应用圆角 // UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds // byRoundingCorners:corners // cornerRadii:CGSizeMake(radius, radius)]; // // // 创建 CAShapeLayer 并设置 path // CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; // maskLayer.path = path.CGPath; // self.layer.mask = maskLayer; // // // 设置边框 // if (borderWidth > 0 && borderColor) { // CAShapeLayer *borderLayer = [[CAShapeLayer alloc] init]; // borderLayer.path = path.CGPath; // borderLayer.lineWidth = borderWidth; // borderLayer.strokeColor = borderColor.CGColor; // borderLayer.fillColor = UIColor.clearColor.CGColor; // borderLayer.frame = self.bounds; // [self.layer addSublayer:borderLayer]; // } //} - (void)setCornerRadius:(CGFloat)radius { self.layer.cornerRadius = radius; self.layer.masksToBounds = YES; // 确保视图内容不会超出边界 } - (void)setCornerRadius:(CGFloat)radius corners:(UIRectCorner)corners { if (corners == UIRectCornerAllCorners) { [self setCornerRadius:radius]; } else { // 如果是部分圆角,使用 `CAShapeLayer` 和 `UIBezierPath`,但仅在必要时使用 UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.path = path.CGPath; self.layer.mask = maskLayer; } } - (void)setCornerRadius:(CGFloat)radius cornerMask:(CACornerMask)cornerMask { self.layer.maskedCorners = cornerMask; self.layer.cornerRadius = radius; self.layer.masksToBounds = YES; } - (void)setCornerRadius:(CGFloat)radius corners:(CACornerMask)corners borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor { // 设置指定角的圆角 self.layer.cornerRadius = radius; self.layer.maskedCorners = corners; self.layer.masksToBounds = YES; // 确保内容不会超出边界 // 设置边框 self.layer.borderWidth = borderWidth; self.layer.borderColor = borderColor.CGColor; } - (void)setAllCornerRadius:(CGFloat)radius borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor { // 设置指定角的圆角 self.layer.cornerRadius = radius; self.layer.maskedCorners = UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight; self.layer.masksToBounds = YES; // 确保内容不会超出边界 // 设置边框 self.layer.borderWidth = borderWidth; self.layer.borderColor = borderColor.CGColor; } @end