UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor blackColor];
[self.view addSubview:label];
label.text = @"这是文字";
label.textColor = [UIColor redColor];
CGSize maxsize = CGSizeMake(320, 100);
//这个字典里面的属性别写错
NSDictionary *atttibute = @{NSFontAttributeName:label.font};
//关键点,根据字体大小和文字长短计算出size
CGSize textSize = [label.text boundingRectWithSize:maxsize options:NSStringDrawingTruncatesLastVisibleLine attributes:atttibute context:nil].size;
CGRect textframe = label.frame;
textframe.size = textSize;
label.frame = textframe;
原文:http://www.cnblogs.com/xj76149095/p/5040149.html