0
点赞
收藏
分享

微信扫一扫

iOS Label 作为分割线时 上下左右切边

言午栩 2023-05-22 阅读 82

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
//    CGFloat borderWidth = 20.0;
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 400, 100, 100)];
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(200, 400, 100, 100)];
    label1.backgroundColor = [UIColor blueColor];
    [self.view addSubview:label1];
//
//    label.backgroundColor = [UIColor cyanColor];
//    [self.view addSubview:label];
//    label.layer.borderWidth = borderWidth;
//    label.layer.borderColor = [UIColor redColor].CGColor;
//    
//    CALayer *mask = [CALayer layer];
//    // The mask needs to be filled to mask
//    [mask setBackgroundColor:[[UIColor blackColor] CGColor]];
//    // Make the masks frame smaller in height
//    CGRect maskFrame = CGRectInset([label bounds], 0, borderWidth/2);
//    // Move the maskFrame to the top
    maskFrame.origin.y = 0;
    maskFrame.origin.y = borderWidth*2;
//    maskFrame.origin.y = borderWidth;
//    [mask setFrame:maskFrame];
//    [[label layer] setMask:mask];
    
    
    
        CGFloat borderWidth = 20.0;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 400, 100, 100)];
//        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(200, 400, 100, 100)];
//        label1.backgroundColor = [UIColor blueColor];
//        [self.view addSubview:label1];
    
        label.backgroundColor = [UIColor yellowColor];
        [self.view addSubview:label];
//    [self clipLayerBorder:label direction:LayerDirectionNone borderWidth:borderWidth borderColor:[UIColor redColor]];
    
//    [self clipLayerBorder:label direction:LayerDirectionLeft borderWidth:borderWidth borderColor:[UIColor redColor]];
    
//    [self clipLayerBorder:label direction:LayerDirectionRight borderWidth:borderWidth borderColor:[UIColor redColor]];
    
    [self clipLayerBorder:label direction:LayerDirectionNone borderWidth:borderWidth borderColor:[UIColor redColor]];
    
}



- (void)clipLayerBorder:(UILabel *)label direction:(LayerClipDirection)direction borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)color
{
//    CGFloat borderWidth = borderWidth;
    
//    label = [[UILabel alloc] initWithFrame:CGRectMake(100, 400, 100, 100)];
    
//    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(200, 400, 100, 100)];
//    label1.backgroundColor = [UIColor blueColor];
//    [self.view addSubview:label1];
    
    label.backgroundColor = [UIColor cyanColor];
//    [self.view addSubview:label];
    label.layer.borderWidth = borderWidth;
    label.layer.borderColor = color.CGColor;
    
    CALayer *mask = [CALayer layer];
    // The mask needs to be filled to mask
    [mask setBackgroundColor:[[UIColor blackColor] CGColor]];
    // Make the masks frame smaller in height
//    CGRect maskFrame = CGRectInset([label bounds], 0, borderWidth/2);
//    // Move the maskFrame to the top
//    //    maskFrame.origin.y = 0;
//    //    maskFrame.origin.y = borderWidth*2;
//    maskFrame.origin.y = borderWidth;
    CGRect maskFrame = CGRectZero;
    if (direction == LayerDirectionUp) {
        maskFrame = CGRectInset([label bounds], 0, borderWidth/2);
        // Move the maskFrame to the top
        //    maskFrame.origin.y = 0;
        //    maskFrame.origin.y = borderWidth*2;
        maskFrame.origin.y = borderWidth;
    }
    
    if (direction == LayerDirectionDown) {
        maskFrame = CGRectInset([label bounds], 0, borderWidth/2);
        // Move the maskFrame to the top
            maskFrame.origin.y = 0;
        //    maskFrame.origin.y = borderWidth*2;
//        maskFrame.origin.y = borderWidth;
    }
    
    if (direction == LayerDirectionLeft) {
        maskFrame = CGRectInset([label bounds], borderWidth/2, 0);
        // Move the maskFrame to the top
        maskFrame.origin.x = borderWidth;
        //    maskFrame.origin.y = borderWidth*2;
        //        maskFrame.origin.y = borderWidth;
    }
    
    if (direction == LayerDirectionRight) {
        maskFrame = CGRectInset([label bounds], borderWidth/2, 0);
        // Move the maskFrame to the top
        maskFrame.origin.x = 0;
        //    maskFrame.origin.y = borderWidth*2;
        //        maskFrame.origin.y = borderWidth;
    }
    
    if (direction == LayerDirectionNone) {
        maskFrame = CGRectInset([label bounds], borderWidth/2, borderWidth/2);
        //切掉右边和下边
        maskFrame.origin.x = 0;
        maskFrame.origin.y = 0;

        //切掉左边和上边
//        maskFrame.origin.x = borderWidth;
//        maskFrame.origin.y = borderWidth;
        
//        maskFrame = CGRectInset([label bounds], 0, 0);
    }
    
    
    [mask setFrame:maskFrame];
    [[label layer] setMask:mask];
}

举报

相关推荐

0 条评论