UILabel *textLabel = [[UILabel alloc] init];
textLabel.font = [UIFont systemFontOfSize:16];
NSString *str = @"2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222";
textLabel.text = str;
textLabel.backgroundColor = [UIColor redColor];
textLabel.numberOfLines = 0;//根據(jù)最大行數(shù)需求來(lái)設(shè)置(0就是不做行數(shù)限制)
textLabel.lineBreakMode = NSLineBreakByTruncatingTail;
CGSize maximumLabelSize = CGSizeMake(100, 9999);//labelsize的最大值
//計(jì)算高度
CGSize expectSize = [textLabel sizeThatFits:maximumLabelSize];
//別忘了把frame給回label崭参,如果用xib加了約束的話(huà)可以只改一個(gè)約束的值
textLabel.frame = CGRectMake(20, 70, expectSize.width, expectSize.height);
[self.view addSubview:textLabel];
// view切圓角
textLabel.layer.cornerRadius = 39;
textLabel.layer.masksToBounds = YES;
//設(shè)置邊框及邊框顏色
textLabel.layer.borderWidth = 8;
textLabel.layer.borderColor =[ [UIColor grayColor] CGColor];
//切圓角
//設(shè)置所需的圓角位置以及大小
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:aView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = aView.bounds;
maskLayer.path = maskPath.CGPath;
aView.layer.mask = maskLayer;
});
/*
代碼量偏多妻味,且很多 UIView 都是使用約束布局丧蘸,必須搭配 dispatch_after 函數(shù)來(lái)設(shè)置自身的 mask蚀之。因?yàn)橹挥性诖藭r(shí),才能把 UIView 的正確的 bounds 設(shè)置到 CAShapeLayer 的 frame 上日缨。
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
};
*/
//設(shè)置邊框線(xiàn)
//設(shè)置陰影
containerView.backgroundColor = [UIColor clearColor];
containerView.layer.shadowOffset = CGSizeMake(0, 2);
containerView.layer.shadowOpacity = 0.80;
END