最近的需求中有個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