在很多情景下都會遇到根據(jù)文字的內(nèi)容來動態(tài)計算一個控件的大小。
本文以固定控件寬度,動態(tài)計算文字高度為示例,其它情況可以同理。
計算一段文字的高度需要固定控件顯示寬度,以及確定文字的字體。
創(chuàng)建一個NSString的分類,方法中需要傳入字體以及寬度兩個參數(shù)。
如果文字段落設置了行間距,那么計算高度的時候也要設置行間距的屬性。
下面是一個實現(xiàn)了該功能的NSString分類
@interface NSString (ZCLSize)
- (CGFloat)zcl_heightWithFont:(UIFont *)font constrainedToWidth:(CGFloat)width;
- (CGFloat)zcl_heightWithFont:(UIFont *)font constrainedToWidth:(CGFloat)width lineSpacing:(CGFloat)lineSpacing;
@end
@implementation NSString (ZCLSize)
- (CGFloat)zcl_heightWithFont:(UIFont *)font constrainedToWidth:(CGFloat)width {
UIFont *textFont = font ? font : [UIFont systemFontOfSize:[UIFont systemFontSize]];
CGSize textSize;
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary *attributes = @{NSFontAttributeName: textFont,
NSParagraphStyleAttributeName: paragraph};
textSize = [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingTruncatesLastVisibleLine)
attributes:attributes
context:nil].size;
return ceil(textSize.height);
}
- (CGFloat)zcl_heightWithFont:(UIFont *)font constrainedToWidth:(CGFloat)width lineSpacing:(CGFloat)lineSpacing {
UIFont *textFont = font ? font : [UIFont systemFontOfSize:[UIFont systemFontSize]];
CGSize textSize;
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.lineBreakMode = NSLineBreakByWordWrapping;
paragraph.lineSpacing = lineSpacing;
NSDictionary *attributes = @{NSFontAttributeName: textFont,
NSParagraphStyleAttributeName: paragraph};
textSize = [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingTruncatesLastVisibleLine)
attributes:attributes
context:nil].size;
return ceil(textSize.height);
}
@end
使用示例:
文字沒有設置行間距。
NSString *showText = @"我不是詩人,所以,只能夠把愛你寫進程序,\n當作不可解的密碼,作為我一個人知道的秘密。\n我以為你是我的唯一,過了很久才發(fā)現(xiàn),你不是我獨占的服務器,\n我可以傳遞,卻什么都不能夠取回,大師說,此算法不可逆。\n我想析構(gòu)我自己,卻沒有多少勇氣,只能夠注釋掉關于你的記憶,\n想尋找你的信息,突然發(fā)現(xiàn),你已經(jīng)不在我的域。";
UILabel *labelContent = [[UILabel alloc]initWithFrame:CGRectMake(14, 64, CGRectGetWidth(self.view.frame) - 28, CGRectGetHeight(self.view.frame) - 64 - 49)];
[self.view addSubview:labelContent];
labelContent.numberOfLines = 0;
[labelContent setTextColor:[UIColor blackColor]];
[labelContent setFont:[UIFont systemFontOfSize:15]];
[labelContent.layer setBorderWidth:1.0];
[labelContent.layer setBorderColor:[UIColor redColor].CGColor];
[labelContent setText:showText];
CGFloat showTextHeight = [showText zcl_heightWithFont:labelContent.font constrainedToWidth:CGRectGetWidth(self.view.frame) - 28];
labelContent.frame = CGRectMake(14, 64, CGRectGetWidth(self.view.frame) - 28, showTextHeight);

Paste_Image.png
文字設置了行間距
NSString *showText = @"我不是詩人,所以,只能夠把愛你寫進程序,\n當作不可解的密碼,作為我一個人知道的秘密。\n我以為你是我的唯一,過了很久才發(fā)現(xiàn),你不是我獨占的服務器,\n我可以傳遞,卻什么都不能夠取回,大師說,此算法不可逆。\n我想析構(gòu)我自己,卻沒有多少勇氣,只能夠注釋掉關于你的記憶,\n想尋找你的信息,突然發(fā)現(xiàn),你已經(jīng)不在我的域。";
UILabel *labelContent = [[UILabel alloc]initWithFrame:CGRectMake(14, 64, CGRectGetWidth(self.view.frame) - 28, CGRectGetHeight(self.view.frame) - 64 - 49)];
[self.view addSubview:labelContent];
labelContent.numberOfLines = 0;
[labelContent setTextColor:[UIColor blackColor]];
[labelContent setFont:[UIFont systemFontOfSize:15]];
[labelContent.layer setBorderWidth:1.0];
[labelContent.layer setBorderColor:[UIColor redColor].CGColor];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:showText];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:12];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [showText length])];
labelContent.attributedText = attributedString;
CGFloat showTextHeight = [showText zcl_heightWithFont:labelContent.font constrainedToWidth:CGRectGetWidth(self.view.frame) - 28 lineSpacing:12];
labelContent.frame = CGRectMake(14, 64, CGRectGetWidth(self.view.frame) - 28, showTextHeight);

Paste_Image.png
總結(jié)
- 本文給出了固定文字顯示寬度以及文字字體,動態(tài)計算文字顯示高度的方法。
- 同理可以得出固定顯示高度,動態(tài)計算文字寬度的方法。
- 示例中同時也給出了怎樣設置文字的行間距的方法。另一篇博客中也有介紹到。http://www.itdecent.cn/p/3bc837edee1b