为了计算UILabel的宽度,除了通过NSString自带的boundingRectWithSize的API外,还可以利用sizeToFit对UILabel封装一个分类。
1. 需求:
- 根据字符串,字体,计算UILabel宽度
- 根据字符串,字体,宽度,计算UILabel高度
2. 解决:
- 新建一个分类Category,封装好相关计算方法
- 调用时,导入该分类,调用相关计算方法得出数值
3. 实现示例:
UILabel+Size分类,实现代码
- 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. 调用示例:
场景:在一个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);
参数解释
- size:
宽高限制,用于计算文本绘制时占据的矩形块。 - options:
文本绘制时的附加选项。可能取值请参考“NSStringDrawingOptions”。 - attributes:
文本绘制时用到的AttributedString的属性。 - context:
context上下文。包括一些信息,例如如何调整字间距以及缩放。最终,该对象包含的信息将用于文本绘制。该参数一般为 nil 。 - 返回值:
一个矩形,大小等于文本绘制完将占据的宽和高。
练习题:封装一个根据字体,字符串,宽度等参数得到高度的方法?
- 参考答案:
-(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的区别
关于boundingRectWithSize,系统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;
网友评论