這里只針對iOS 7后的動(dòng)態(tài)計(jì)算文本寬高方法進(jìn)行總結(jié),也就是下文中的方法:
// for NSString
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSString *, id> *)attributes context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 7_0);
以及
// for NSAttributedString
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 6_0);
首先,我們先來講講NSString的動(dòng)態(tài)計(jì)算寬高的方法
NSString* str = @"這是一個(gè)測試文本這是一個(gè)測試文本這是一個(gè)測試文本這是一個(gè)測試文本這是一個(gè)測試文本這是一個(gè)測試文本這是一個(gè)測試文本";
self.label = [[UILabel alloc]init];
/************************這幾項(xiàng)會(huì)影響最后計(jì)算出來的結(jié)果********************************/
self.label.lineBreakMode = NSLineBreakByCharWrapping;
self.label.font = [UIFont systemFontOfSize:15];
// self.label.textAlignment = NSTextAlignmentLeft;
self.label.numberOfLines = 0;
/******************************************************************************/
self.label.layer.borderWidth = 1;
self.label.layer.borderColor = [[UIColor lightGrayColor] CGColor];
self.label.textColor = [UIColor darkGrayColor];
self.label.text = str;
NSDictionary* dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
CGRect rect = [self.label.text boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil];
self.label.frame = CGRectMake(100, 100, rect.size.width, rect.size.height);
[self.view addSubview:self.label];

效果圖
特別注意上面所說的幾項(xiàng)影響最后計(jì)算出來的寬高的因素,例如一開始我就因?yàn)闆]有指定label的字體大小,導(dǎo)致顯示一直有問題,找了半天才找到。
其次,我們先來講講NSAttributedString的動(dòng)態(tài)計(jì)算寬高的方法
//設(shè)置一個(gè)段落格式
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.lineSpacing = 10;
paragraph.firstLineHeadIndent = 20.f;
NSDictionary* dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor darkGrayColor],NSParagraphStyleAttributeName:paragraph};
self.label = [[UILabel alloc]init];
self.label.layer.borderWidth = 1;
self.label.layer.borderColor = [[UIColor lightGrayColor] CGColor];
self.label.lineBreakMode = NSLineBreakByCharWrapping;
self.label.numberOfLines = 0;
self.label.text = @"這是一個(gè)測試文本\n這是一個(gè)測試文本這是一個(gè)測試文本這是一個(gè)測試文本這是一個(gè)測試文本這是一個(gè)測試文本這是一個(gè)測試文本";
NSRange allRange = NSMakeRange(0, self.label.text.length);
[self.view addSubview:self.label];
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:self.label.text];
[attrStr addAttributes:dic range:allRange];
//NSMutableAttributedString的動(dòng)態(tài)計(jì)算寬高方法
CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil];
self.label.frame = CGRectMake(100, 100, rect.size.width, rect.size.height);
self.label.attributedText = attrStr;

效果圖
![Uploading 屏幕快照 2017-05-25 上午11.31.29_101947.png . . .]