為了計算UILabel的寬度哄芜,除了通過NSString自帶的boundingRectWithSize的API外貌亭,還可以利用sizeToFit對UILabel封裝一個分類。
1. 需求:
- 根據(jù)字符串认臊,字體圃庭,計算UILabel寬度
- 根據(jù)字符串,字體失晴,寬度剧腻,計算UILabel高度
2. 解決:
- 新建一個分類Category,封裝好相關計算方法
- 調(diào)用時涂屁,導入該分類书在,調(diào)用相關計算方法得出數(shù)值
3. 實現(xiàn)示例:
UILabel+Size分類,實現(xiàn)代碼
- UILabel+Size.h
//
// UILabel+Size.h
// Created by ChenMan on 2018/1/25.
// Copyright ? 2018年. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UILabel (Size)
+ (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString *)title font:(UIFont*)font;
+ (CGFloat)getWidthWithTitle:(NSString *)title font:(UIFont *)font;
@end
- UILabel+Size.m
//
// UILabel+Size.m
// Created by ChenMan on 2018/1/25.
// Copyright ? 2018年. All rights reserved.
//
#import "UILabel+Size.h"
@implementation UILabel (Size)
+ (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString *)title font:(UIFont *)font
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
label.text = title;
label.font = font;
label.numberOfLines = 0;
[label sizeToFit];
CGFloat height = label.frame.size.height;
return ceil(height);
}
+ (CGFloat)getWidthWithTitle:(NSString *)title font:(UIFont *)font {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1000, 0)];
label.text = title;
label.font = font;
[label sizeToFit];
CGFloat width = label.frame.size.width;
return ceil(width);
}
@end
4. 調(diào)用示例:
場景:在一個UITableViewCell中拆又,重寫Cell的一個模型屬性的setter方法儒旬,需要先對UILabel對象的text屬性賦值后,再進行更新布局約束操作遏乔。代碼如下:
- CMTestTableViewCell.m
- (void)setCellMdl:(SupplementCellModel *)cellMdl{
if (cellMdl) {
_cellMdl = cellMdl;
self.titleLabel.text = cellMdl.titleStr;
self.contextTextField.placeholder = cellMdl.holderStr;
self.rightLbl.text = cellMdl.tailStr;
CGFloat width = [UILabel getWidthWithTitle:self.rightLbl.text font:self.rightLbl.font];
[self.rightLbl mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(width);
}];
self.kind = cellMdl.cellType;
}
}
5. 補充拓展
還有一種方式义矛,可利用NSString的API
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSString *, id> *)attributes context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 7_0);
參數(shù)解釋
- size:
寬高限制,用于計算文本繪制時占據(jù)的矩形塊盟萨。 - options:
文本繪制時的附加選項凉翻。可能取值請參考“NSStringDrawingOptions”捻激。 - attributes:
文本繪制時用到的AttributedString的屬性制轰。 - context:
context上下文。包括一些信息胞谭,例如如何調(diào)整字間距以及縮放垃杖。最終,該對象包含的信息將用于文本繪制丈屹。該參數(shù)一般為 nil 调俘。 - 返回值:
一個矩形伶棒,大小等于文本繪制完將占據(jù)的寬和高。
練習題:封裝一個根據(jù)字體彩库,字符串肤无,寬度等參數(shù)得到高度的方法?
- 參考答案:
-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withWidth:(CGFloat)width
{
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.alignment = NSTextAlignmentLeft;
paraStyle.lineSpacing = 4;
NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle
};
CGSize size = [str boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
return size.height;
}
API的區(qū)別
關于boundingRectWithSize骇钦,系統(tǒng)API有幾個類的相關方法宛渐。搜索官方文檔,可見如下:
image.png
其中
- NSAttributedString
Calculates and returns bounding rectangle for the receiver drawn using the options specified, within the given rectangle in the current graphics context.
- (NSRect)boundingRectWithSize:(NSSize)size options:(NSStringDrawingOptions)options;
- NSAttributedString
Returns the bounding rectangle required to draw the string.
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context;
- NSString
Calculates and returns the bounding rect for the receiver drawn using the given options and display characteristics, within the specified rectangle in the current graphics context.
- (NSRect)boundingRectWithSize:(NSSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary<NSAttributedStringKey, id> *)attributes;
- NSString
Calculates and returns the bounding rect for the receiver drawn using the given options and display characteristics, within the specified rectangle in the current graphics context.
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary<NSAttributedStringKey, id> *)attributes context:(NSStringDrawingContext *)context;