UIView自定义四个圆角和边框

该文章详细介绍了如何使用Objective-C在iOS开发中为UIView对象设置自定义圆角和边框,包括使用CALayer和UIBezierPath来实现不同角落的圆角效果,以及CALayer用于添加颜色和宽度的边框。
摘要由CSDN通过智能技术生成
#import "UIView+Border.h"

@implementation UIView (Border)

- (void)cornerRadius:(CGFloat)radius
             topLeft:(BOOL)topLeft
            topRight:(BOOL)topRight
          bottomLeft:(BOOL)bottomLeft
         bottomRight:(BOOL)bottomRight {
    
    [self cornerRadius:radius inRect:self.bounds topLeft:topLeft topRight:topRight bottomLeft:bottomLeft bottomRight:bottomRight];
}

// 自定义圆角, 指定圆角区域
- (void)cornerRadius:(CGFloat)radius
              inRect:(CGRect)inRect
             topLeft:(BOOL)topLeft
            topRight:(BOOL)topRight
          bottomLeft:(BOOL)bottomLeft
         bottomRight:(BOOL)bottomRight {
    
    if (@available(iOS 11.0, *))
    {
        CACornerMask cornerMask = 0;
        if (topLeft) {
            cornerMask |= kCALayerMinXMinYCorner;
        }
        if (topRight) {
            cornerMask |= kCALayerMaxXMinYCorner;
        }
        if (bottomLeft) {
            cornerMask |= kCALayerMinXMaxYCorner;
        }
        if (bottomRight) {
            cornerMask |= kCALayerMaxXMaxYCorner;
        }
        self.layer.maskedCorners = cornerMask;
        self.layer.cornerRadius = radius;
    }
    else
    {
        UIRectCorner corner = 0;
        if (topLeft) {
            corner |= UIRectCornerTopLeft;
        }
        if (topRight) {
            corner |= UIRectCornerTopRight;
        }
        if (bottomLeft) {
            corner |= UIRectCornerBottomLeft;
        }
        if (bottomRight) {
            corner |= UIRectCornerBottomRight;
        }
        
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = inRect;
        maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:inRect byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)].CGPath;
        self.layer.mask = maskLayer;
    }
}

// 自定义边框
- (void)borderColor:(UIColor *)color
              width:(CGFloat)width
                top:(BOOL)top
               left:(BOOL)left
             bottom:(BOOL)bottom
              right:(BOOL)right {
    if (top) {
        [self addTopBorderWithColor:color andWidth:width];
    }
    if (left) {
        [self addLeftBorderWithColor:color andWidth:width];
    }
    if (bottom) {
        [self addBottomBorderWithColor:color andWidth:width];
    }
    if (right) {
        [self addRightBorderWithColor:color andWidth:width];
    }
}

// ----------------------------------
- (void)addTopBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth
{
    CALayer *borderLayer = [self getBorderLayer:@"topBorder"];
    borderLayer.frame = CGRectMake(0, 0, self.frame.size.width, borderWidth);
    [self setBorder:borderLayer color:color width:borderWidth];
}

- (void)addBottomBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth
{
    CALayer *borderLayer = [self getBorderLayer:@"bottomBorder"];
    borderLayer.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, borderWidth);
    [self setBorder:borderLayer color:color width:borderWidth];
}

- (void)addLeftBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth
{
    CALayer *borderLayer = [self getBorderLayer:@"leftBorder"];
    borderLayer.frame = CGRectMake(0, 0, borderWidth, self.frame.size.height);
    [self setBorder:borderLayer color:color width:borderWidth];
}

- (void)addRightBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth
{
    CALayer *borderLayer = [self getBorderLayer:@"rightBorder"];
    borderLayer.frame = CGRectMake(self.frame.size.width - borderWidth, 0, borderWidth, self.frame.size.height);
    [self setBorder:borderLayer color:color width:borderWidth];
}


static const char borderLayersKey = '\0';
- (NSMutableDictionary *)borderLayers {
    NSMutableDictionary *dict = objc_getAssociatedObject(self, &borderLayersKey);
    if (!dict) {
        dict = [NSMutableDictionary dictionary];
        objc_setAssociatedObject(self, &borderLayersKey, dict, OBJC_ASSOCIATION_RETAIN);
    }
    return dict;
}

- (CALayer *)getBorderLayer:(NSString *)key {
    NSMutableDictionary *borderLayers = [self borderLayers];
    CALayer *layer = borderLayers[key];
    if (!layer) {
        layer = [CALayer layer];
        borderLayers[key] = layer;
    }
    return layer;
}

- (void)setBorder:(CALayer *)borderLayer color:(UIColor *)color width:(CGFloat)width {
    CGFloat alpha;
    [color getRed:nil green:nil blue:nil alpha:&alpha];
    if (alpha <= 0 || width <= 0) {
        [borderLayer removeFromSuperlayer];
    } else {
        borderLayer.backgroundColor = color.CGColor;
        [self.layer addSublayer:borderLayer];
    }
}

@end

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值