最近的需求中有個label需要設定右下角為圓角,其餘三個為直角,一開始用的是重寫drawRect,然後用繪圖重繪每個角的樣子,計算起來還是麻煩
後來發現了下面的方法:
//以一個UILabel為例,只讓右下角為圓弧
UILabel *courseStyleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 37, 16)];
courseStyleLabel.backgroundColor = CreateColorByRGB(TitleBgImgViewColor);
courseStyleLabel.textColor = [UIColor whiteColor];
courseStyleLabel.textAlignment = NSTextAlignmentCenter;
courseStyleLabel.font = [UIFont systemFontOfSize:12.0];
courseStyleLabel.layer.masksToBounds = YES;
//這裡設定需要繪製的圓角
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:courseStyleLabel.bounds
byRoundingCorners:UIRectCornerBottomRight//右下角
cornerRadii:CGSizeMake(3, 3)];//設定圓角大小,弧度為3
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = courseStyleLabel.bounds;
maskLayer.path = maskPath.CGPath;
courseStyleLabel.layer.mask = maskLayer;
[courseImage addSubview:courseStyleLabel];
複製程式碼
通過這幾個列舉值判斷畫哪個圓角
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
複製程式碼