方式1:
设置好label的文字,字号等属性后,调用
self.label.text = @"111";
self.label.font = [UIFont systemFontOfSize:17];
[self.label sizeToFit];
方式2:
使用字符串的方法计算文字宽度,
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context;
对比2种方法的计算计算效率, 直接看结论, 使用boundingRectWithSize效率更高:
使用方法 | 10W个字符串 | sizeToFit | 3.18s | boundingRectWithSize | 2.26s |
其中NSStringDrawingOptions有四个枚举值:
typedef ?NS_OPTIONS ( NSInteger ,? NSStringDrawingOptions ) {
?? // 绘制文本时使用 line fragement origin 而不是 baseline origin
? //? 整个文本将以每行组成的矩形为单位计算整个文本的尺寸
???? NSStringDrawingUsesLineFragmentOrigin ?= 1 << 0,? // The specified origin is the line fragment origin, not the base line origin
?? // 计算行高时使用行距。(译者注:字体大小+行间距=行距)
???? NSStringDrawingUsesFontLeading ?= 1 << 1,? // Uses the font leading for calculating line heights
??// 计算布局时使用图元字形(而不是印刷字体)。 也即是以每一个字体所占用的空间来计算文本范围
???? NSStringDrawingUsesDeviceMetrics ?= 1 << 3,? // Uses image glyph bounds instead of typographic bounds
// 如果文本内容超出指定的矩形限制,文本将被截去并在最后一个字符后加上省略号。如果没有指定NSStringDrawingUsesLineFragmentOrigin选项,则该选项被忽略
???? NSStringDrawingTruncatesLastVisibleLine ?= 1 << 5,? // Truncates and adds the ellipsis character to the last visible line if the text doesn't fit into the bounds specified. Ignored if NSStringDrawingUsesLineFragmentOrigin is not also set.
}? NS_ENUM_AVAILABLE_IOS (6_0);
attributes是文本字体的属性:该参数要设置字体的大小。
context是上下文对象,用于包含信息:如何调整字间距以及缩放。最终,该对象包含的信息将用于文本绘制。该参数可为 nil。?
性能测试代码:
- (void)viewDidLoad {
[super viewDidLoad];
NSInteger count = 10 * 10000;
NSMutableArray *array = [NSMutableArray arrayWithCapacity:count];
NSString *path = [[NSBundle mainBundle] pathForResource:@"log" ofType:@"log"];
NSString *str = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSArray *temp = [str componentsSeparatedByString:@"\n"];
while (array.count < count) {
[array addObjectsFromArray:temp];
}
NSLog(@"总计字符串数量:%zd",array.count);
NSTimeInterval start = CACurrentMediaTime();
for (NSInteger i = 0; i < array.count; i++) {
self.label.text = array[i];
[self.label sizeToFit];
}
NSTimeInterval end = CACurrentMediaTime();
NSLog(@"sizeToFit %.2f",end-start); // 3.24s
start = CACurrentMediaTime();
for (NSInteger i = 0; i < array.count; i++) {
CGSize textSize = CGSizeZero;
NSDictionary *attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
textSize = [array[i] boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 40)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:attribute
context:nil].size;
textSize.width = ceilf(textSize.width);
}
end = CACurrentMediaTime();
NSLog(@"boundingRectWithSize %.2f",end-start); // 2.25s
for (NSInteger i = 0; i < array.count; i++) {
self.label.text = array[i];
[self.label sizeToFit];
CGSize textSize = CGSizeZero;
NSDictionary *attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
textSize = [array[i] boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 40)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:attribute
context:nil].size;
textSize.width = ceilf(textSize.width);
if (ABS(self.label.frame.size.width - textSize.width)>1 ) {
NSLog(@"出错计算错误, %.2f %.2f",self.label.frame.size.width,textSize.width);
}
}
NSLog(@"%s结束",__func__);
}
?
?
|