UIView设置少于四个的圆角

  最近的需求中有个label需要设置右下角为圆角,其余三个为直角,一开始用的是重写drawRect,然后用绘图重绘每个角的样子,计算起来还是麻烦

  后来发现了下面的方法:

  

 1 UILabel *courseStyleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 37, 16)];
 2         courseStyleLabel.backgroundColor = CreateColorByRGB(TitleBgImgViewColor);
 3         courseStyleLabel.textColor = [UIColor whiteColor];
 4         courseStyleLabel.textAlignment = NSTextAlignmentCenter;
 5         courseStyleLabel.font = [UIFont systemFontOfSize:12.0];
 6         courseStyleLabel.layer.masksToBounds = YES;
 7 
 8         UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:courseStyleLabel.bounds
 9                                                        byRoundingCorners:UIRectCornerBottomRight
10                                                              cornerRadii:CGSizeMake(3, 3)];
11         CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
12         maskLayer.frame = courseStyleLabel.bounds;
13         maskLayer.path = maskPath.CGPath;
14         courseStyleLabel.layer.mask = maskLayer;
15         [courseImage addSubview:courseStyleLabel];

  显示效果是右下角有个 弧度为 3 的小圆角

  

UIRectCornerTopLeft     = 1 << 0,
    UIRectCornerTopRight    = 1 << 1,
    UIRectCornerBottomLeft  = 1 << 2,
    UIRectCornerBottomRight = 1 << 3,
    UIRectCornerAllCorners  = ~0UL

  通过这个枚举值判断画哪个圆角

posted @ 2016-04-21 10:34  黑暗森林的歌者  阅读(291)  评论(0编辑  收藏  举报