根据Label和字体大小自适应高度
- (void)getHeightWithLabel:(UILabel *)label andFontSize:(CGFloat)size
{
label.numberOfLines = 0;
NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] initWithString:label.text];
[labelString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(0, [labelString length])];
CGRect labelStringRect = [labelString boundingRectWithSize:CGSizeMake(self.view.frame.size.width-label.frame.origin.x*2, 9999) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGRect labelRect = label.frame;
labelRect.size.height = labelStringRect.size.height;
label.frame = labelRect;
label.attributedText = labelString;
}
根据Label和字体大小自适应宽度
- (void)getWidthWithLabel:(UILabel *)label andFontSize:(CGFloat)size
{
label.numberOfLines = 0;
NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] initWithString:label.text];
[labelString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(0, [labelString length])];
CGRect labelStringRect = [labelString boundingRectWithSize:CGSizeMake(9999, label.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGRect labelRect = label.frame;
labelRect.size.width = labelStringRect.size.width;
label.frame = labelRect;
label.attributedText = labelString;
}