// 小于四个角 圆角
-(void)setbor{ NSString *str = @" couldn't fit this all in a comment to @lomanf's answer. So I'm adding it as an answer."; //计算字符高度
[Corner layoutHeightWithLable:self.label text:str]; /* 1.使用空白 view addSubView label 2.得到类似 qq 聊天气泡,仅仅只是类似,还是有区别 */ UIView *view = [[UIView alloc]init]; //view 的 frame 要比 label 大一点,不然切圆角会切到字符串
view.frame = CGRectMake(50 , 50 , _label.frame.size.width + 20 , _label.frame.size.height + 20); view.backgroundColor = [UIColor redColor]; //圆角
[Corner createCornerInView:view corners:UIRectCornerBottomLeft|UIRectCornerTopRight cgsize:CGSizeMake(10, 10)]; [self.view addSubview:view]; [view addSubview:_label]; }
#import <UIKit/UIKit.h>
@interface Corner : UIView +(void)createCornerInView:(UIView *)view corners:(UIRectCorner)corner cgsize:(CGSize)size; +(void)layoutHeightWithLable:(UILabel *)label text:(NSString *)text; @end
+(void)layoutHeightWithLable:(UILabel *)label text:(NSString *)text{ label.numberOfLines = 0; label.lineBreakMode = NSLineBreakByWordWrapping; CGSize maxSize = CGSizeMake(300, MAXFLOAT); CGSize textSize = [text boundingRectWithSize:maxSize options:(NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:label.font} context:nil].size; label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, textSize.width, textSize.height); label.text = text; } +(void)createCornerInView:(UIView *)view corners:(UIRectCorner)corner cgsize:(CGSize)size{ /* typedef NS_OPTIONS(NSUInteger, UIRectCorner) { UIRectCornerTopLeft = 1 << 0, - 左上角 UIRectCornerTopRight = 1 << 1, - 右上角 UIRectCornerBottomLeft = 1 << 2, - 左下角 UIRectCornerBottomRight = 1 << 3, - 右下角 UIRectCornerAllCorners = ~0UL - 四只角 }; */
//贝塞尔曲线
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corner cornerRadii:size]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init]; maskLayer.frame = view.bounds; maskLayer.path = bezierPath.CGPath; view.layer.mask = maskLayer; }