ios 计算label宽度方法效率对比

方式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个字符串
sizeToFit3.18s
boundingRectWithSize2.26s

其中NSStringDrawingOptions有四个枚举值:

typedef NS_OPTIONS(NSIntegerNSStringDrawingOptions) {

  // 绘制文本时使用 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__);

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值