開發中經常會去計算一段文本的高度和寬度,常用的方法一般有兩個:
第一個:
- (CGSize)sizeWithAttributes:(nullable NSDictionary *)attrs NS_AVAILABLE(10_0, 7_0);
包含一個NSDictionary類型的參數Attributes
key值可以指定:NSFontAttributeName(字體) 、NSParagraphStyleAttributeName(段落)等;
id是key對應的值,比如字體UIFont,段落NSParagraphStyle等等。
實例:
//定義一個字符串
static NSString *const kTestString1 = @"我的發小蘇禾是兜兜轉轉一圈后,\n嫁給梁凱的。\n我的發小蘇禾是兜兜轉轉一圈后,嫁給梁凱的。";
///然后計算size
//label1
CGSize textSize1 = [kTestString1 sizeWithAttributes:@{NSFontAttributeName:self.testLabel1.font}];
self.testLabel1.text = kTestString1;
self.testLabel1.frame = CGRectMake(0, 100, textSize1.width, textSize1.height);
文本計算結果:
顯示效果1.png
這個方法使用的時候如果有一段文字計算結果的width超過了屏幕的寬度那么顯示效果會超出屏幕,所以一般用于較短的文本計算。
Tip:可以動態創建一個button或者label的時候可以根據文字寬度來指定frame。像很多搜索界面下面的標簽就可以使用這個方法來實現
顯示效果2.png
如果想要更為詳細的文本計算的話,上面的方法達明顯不滿足要求。在實際開發過程中很多地方會用到動態獲取文字高度的情況,比如表格Cell的高度,HUD上面的label大小等等,我們可以使用下面的方法來達到效果。
第二個:
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary *)attributes context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 7_0);
里面包括四個參數size、options、attributes、context。
size:一個指定的矩形的大小,一般我們會指定一個矩形區域的Size,比如CGSizeMake(100, 100),具體的值是根據需要來設置。
我一般使用CGSizeMake ('需要的寬度',CGFLOAT_MAX),這樣會得到一個指定寬度動態高度的size。
options:這個是一個NS_OPTIONS的枚舉表示計算的類型
包括:
1.NSStringDrawingUsesLineFragmentOrigin:繪制文本時使用 line fragement origin 而不是 baseline origin。一般使用這項
2.NSStringDrawingUsesFontLeading:根據字體計算高度
3.NSStringDrawingUsesDeviceMetrics:使用象形文字計算高度
4.NSStringDrawingTruncatesLastVisibleLine:這個目前我沒怎么用過
一般使用NSStringDrawingUsesFontLeading
和NSStringDrawingUsesLineFragmentOrigin的組合
attribute:和上面介紹的一樣;
context:包括一些信息,例如如何調整字間距以及縮放。該參數一般可為 nil 。
實例:
CGSize textSize2 = [kTestString2 boundingRectWithSize:CGSizeMake(kWidth - 20, CGFLOAT_MAX)
options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:self.testLabel2.font}
context:nil].size;
self.testLabel2.text = kTestString2;
self.testLabel2.frame = CGRectMake(10, 200, textSize2.width, textSize2.height);
運行結果如圖:
顯示效果3.png
第二個方法除了用于普通文本計算以外,還可以計算attributeString的計算,上代碼:
static NSString *const kTestTitle = @"這是一個測試標題\n";
static NSString *const kTestString3 = @"我的發小蘇禾是兜兜轉轉一圈后,嫁給梁凱的。讀書的時候,梁凱暗戀蘇禾。可蘇禾是班花呀,身邊圍著一堆男生,梁凱有點不起眼。關于喜歡的話,梁凱從來沒敢說出口。畢業后,蘇禾考了公務員。工作穩定,人又漂亮,走到哪都是風景。遺憾的是,她始終沒有遇到可以塵埃落定的良人。談了一場又一場戀愛后,漸漸有些心灰意冷\n\n我的發小蘇禾是兜兜轉轉一圈后,嫁給梁凱的。讀書的時候,梁凱暗戀蘇禾。可蘇禾是班花呀,身邊圍著一堆男生,梁凱有點不起眼。關于喜歡的話,梁凱從來沒敢說出口。畢業后,蘇禾考了公務員。工作穩定,人又漂亮,走到哪都是風景。遺憾的是,她始終沒有遇到可以塵埃落定的良人。談了一場又一場戀愛后,漸漸有些心灰意冷";
//1 創建一個attributeString
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] init];
//標題
NSRange titleRange = NSMakeRange(0, kTestTitle.length);
NSMutableAttributedString *titleAttributeString = [[NSMutableAttributedString alloc] initWithString:kTestTitle];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentCenter;
[titleAttributeString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:titleRange];
[titleAttributeString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:18.f] range:titleRange];
[attributeString appendAttributedString:titleAttributeString];
//內容
NSRange contentRange = NSMakeRange(0, kTestString3.length);
NSMutableAttributedString *contentAttributeString = [[NSMutableAttributedString alloc] initWithString:kTestString3];
NSMutableParagraphStyle *contentParagraphStyle = [[NSMutableParagraphStyle alloc] init];
contentParagraphStyle.lineSpacing = 10.f;
[contentAttributeString addAttribute:NSParagraphStyleAttributeName value:contentParagraphStyle range:contentRange];
[contentAttributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.f] range:contentRange];
[contentAttributeString addAttribute:NSKernAttributeName value:@(1) range:contentRange];
[attributeString appendAttributedString:contentAttributeString];
//計算標題size
CGSize size = CGSizeMake(kWidth - 20, CGFLOAT_MAX);
NSDictionary *titleAttribute = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18.f], NSParagraphStyleAttributeName:paragraphStyle};
CGSize titleSize = [kTestTitle boundingRectWithSize:size
options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
attributes:titleAttribute
context:nil].size;
//計算內容size
NSDictionary *contentAttribute = @{NSFontAttributeName:[UIFont systemFontOfSize:14.f],
NSParagraphStyleAttributeName:contentParagraphStyle,
NSKernAttributeName:@(1)};
CGSize contentSize = [kTestString3 boundingRectWithSize:size
options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
attributes:contentAttribute
context:nil].size;
self.testLabel3.attributedText = attributeString;
self.testLabel3.frame = CGRectMake(10, 100, size.width, titleSize.height + contentSize.height);
[self.view addSubview:self.testLabel3];
運行結果:
顯示效果4.png
先就這么多內容,一般情況下計算文本基本上也就這樣了,后續會補充一下圖文計算。
如果您有意見或者建議歡迎留言評論
作者:ForKid
鏈接:http://www.lxweimin.com/p/7388ef05f32c
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。