label自适应高度
1、自定义方法
+(CGFloat)heightForName:(NSString *)string;
2、实现自定义方法
+ (CGFloat)heightForName:(NSString *)string
{
//宽度和label宽度相同
CGSize size = CGSizeMake(WIDTH - 120, 300);
NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:11] forKey:NSFontAttributeName];
CGRect frame = [string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
return frame.size.height;
}
3、计算的长度赋值给label
CGRect nameFrame = self.nameLabel.frame;
nameFrame.size.height = [[self class] heightForName:self.nameLabel.text];
self.nameLabel.frame = nameFrame;