最近的需求中有个label须要设置右下角为圆角,其他三个为直角,一开始用的是重写drawRect,而后用绘图重绘每一个角的样子,计算起来仍是麻烦spa
后来发现了下面的方法:code
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 的小圆角blog
UIRectCornerTopLeft = 1 << 0, UIRectCornerTopRight = 1 << 1, UIRectCornerBottomLeft = 1 << 2, UIRectCornerBottomRight = 1 << 3, UIRectCornerAllCorners = ~0UL
经过这个枚举值判断画哪一个圆角it