label的宽度自适应
约束上下左 右边不约束,这时候label的宽度就可以随着label自适应
效果如下图
如果用frame的话
//优惠图标
UILabel *PreferL = [[UILabel alloc] initWithFrame:CGRectZero];
CGSize size = CGSizeMake(100,2000);
PreferL.backgroundColor = UIColorFromRGB(0xFFEEE9);
PreferL.textColor = UIColorFromRGB(0xFF5823);
PreferL.textAlignment = NSTextAlignmentCenter;
PreferL.text = @"满减";//group.activityName;
PreferL.font = FONT(13);
PreferL.layer.cornerRadius = 7;
PreferL.clipsToBounds = YES;
CGSize labelsize = [PreferL.text boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName : FONT(13)} context:nil].size;
[PreferL setFrame:CGRectMake(11,30/2-18/2, labelsize.width+4, 18)];
[bgView addSubview:PreferL];
网友评论