美文网首页iOS学习笔记
UILabel切圆角,另一半不切圆角

UILabel切圆角,另一半不切圆角

作者: 香橙柚子 | 来源:发表于2017-06-25 23:50 被阅读39次

    今天项目提出一个新需求,需要将一个label右边切圆角,左边不切,实现了以下,共享一下代码.

    UILabel * JHLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 60, 160 , 60)];
    
    JHLabel.backgroundColor = [UIColor lightGrayColor];
    JHLabel.textAlignment = NSTextAlignmentCenter;
    JHLabel.textColor = [UIColor whiteColor];
    JHLabel.text = @"121212";
    // JHLabel.layer.masksToBounds = YES;
    // JHLabel.layer.cornerRadius = 10;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:JHLabel.bounds byRoundingCorners:UIRectCornerBottomRight | UIRectCornerTopRight cornerRadii:CGSizeMake(30, 30)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = JHLabel.bounds;
    maskLayer.path = maskPath.CGPath;
    JHLabel.layer.mask  = maskLayer;
    [self.view addSubview:JHLabel];
    
    效果图

    相关文章

      网友评论

        本文标题:UILabel切圆角,另一半不切圆角

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