UIView方法之SizeToFit

作用: 计算出最优size, 并且改变UIView的size

Demo1: 高度不变,宽度随文本大小变化而变化

设置字号为13,使用SizeToFit自适应结果为

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 100, 40)];
    label.backgroundColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:13];
    label.text = @"天青色等烟雨\n而我在等你\n炊烟袅袅升起\n隔江千万里";
    [self.view addSubview:label];
    [label sizeToFit];
    NSLog(@"frame = %@", NSStringFromCGRect(label.frame));

UILabel打印结果为:

frame = {{20, 100}, {307, 16}}

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 100, 100, 40)];
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    [btn setTitle:@"天青色等烟雨\n而我在等你\n炊烟袅袅升起\n隔江千万里" forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn sizeToFit];
    NSLog(@"frame = %@", NSStringFromCGRect(btn.frame));

UIButton打印结果为:

frame = {{20, 100}, {540, 28}}

UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 100, 40)];
    field.backgroundColor = [UIColor whiteColor];
    field.text = @"天青色等烟雨\n而我在等你\n炊烟袅袅升起\n隔江千万里";
    field.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:field];
    [field sizeToFit];
    NSLog(@"frame = %@", NSStringFromCGRect(field.frame));

UITextField打印结果为:

frame = {{20, 100}, {544, 16}}

此时三个控件都是单行模式下,高度不变,宽度随文本大小变化而变化.

但UILabel是有设置多行功能的,所以UILabel和UIButton均可设置多行,请看Demo2

Demo2: 宽度不变,高度随文本大小变化而变化

设置宽度为100,UILabel的numberOfLines大于1行,假如为3行

若自适应后的宽度小于100,则宽度小于100,高为一行

若是自适应后的宽度大于100,则宽度为100,若高度大于3行,则高度为3行,剩余内容不显示,若高度小于三行,则高度为计算出的高度

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 100, 40)];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:13];
    label.numberOfLines = 3;
    label.text = @"天青色等烟雨\n而我在等你\n炊烟袅袅升起\n隔江千万里";
    [self.view addSubview:label];
    [label sizeToFit];
    NSLog(@"frame = %@", NSStringFromCGRect(label.frame));

UIlabel打印结果为:

frame = {{20, 100}, {97, 47}}

Demo3: 宽度不变,高度随文本大小变化而变化

设置宽度为100,UILabel的numberOfLines为0行

若自适应后的宽度小于100,则宽度小于100,高为一行

若是自适应后的宽度大于100,则宽度为100,高度为自适应后的高度

注意:高度自适应时,宽度不可设置为0,若为0,则变成了单行宽度自适应

但刚刚自适应算出来的坐标,上下左右均紧贴着Label的边,不美观,可做如下修饰

label.width += 10;
label.height += 10;

UIView方法之sizeThatFits

作用: 计算出最优size, 但是不会改变UIView的size

用法: 将sizeThatFits的宽高设置较大点,会在这个范围内自动计算出最匹配宽高

若计算的宽度小于设置的宽度,则以计算出来的高度为准

若计算的宽度大于设置的宽度,则以设置的宽度去进行高度自适应

注意:若要自适应,要重设Label坐标

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 100, 20)];
    label.backgroundColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:13];
    label.numberOfLines = 0;
    label.text = @"天青色等烟雨\n而我在等你\n炊烟袅袅升起\n隔江千万里";
    [self.view addSubview:label];
    CGSize sizeThat = [label sizeThatFits:CGSizeMake(1000, 1000)];
    label.frame = CGRectMake(20, 100, sizeThat.width, sizeThat.height);
    NSLog(@"frame = %@", NSStringFromCGSize(sizeThat));

UIlabel打印结果为:(因为有换行,所以计算出)

frame = {80, 62.5}

NSString方法之boundingRectWithSize

_boundingRectWithSizeLabel.numberOfLines = 0;
    _boundingRectWithSizeLabel.font = [UIFont systemFontOfSize:18];

    NSString *strText = @"这是一段很长的文字,你需要计算这个高度到底是多少";
    _boundingRectWithSizeLabel.text = strText;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:[UIFont systemFontOfSize:18] forKey:NSFontAttributeName];
    CGSize size = [strText boundingRectWithSize:CGSizeMake(100, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    _boundingRectWithSizeLabel.frame = CGRectMake(15, _h5TextView.bottom + 30, size.width, size.height);

NSAttributedString方法之boundingRectWithSize

_boundingRectWithSizeAttributedLabel.numberOfLines = 0;
    _boundingRectWithSizeAttributedLabel.font = [UIFont systemFontOfSize:13];

    NSString *h5boundingRectWithSizeString = @"<p>天青色等烟雨,而我在等你,炊烟袅袅升起,隔江千万里,在瓶底书汉隶仿前朝的飘逸,就当我为遇见你伏笔,天青色等烟雨,而我在等你,月色被打捞起,晕开了结局,如传世的青花瓷自顾自美丽,你眼带笑意,色白花青的锦鲤跃然於碗底,临摹宋体落款时却惦记着你,你隐藏在窑烧里千年的秘密,极细腻犹如绣花针落地,帘外芭蕉惹骤雨,门环惹铜绿,而我路过那江南小镇惹了你,在泼墨山水画里,你从墨色深处被隐去</p>";
    NSMutableAttributedString *h5boundingRectWithSizeAttributedString = [[NSMutableAttributedString alloc] initWithData:[h5boundingRectWithSizeString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    _boundingRectWithSizeAttributedLabel.attributedText = h5boundingRectWithSizeAttributedString;

    CGSize size1 = [h5boundingRectWithSizeAttributedString boundingRectWithSize:CGSizeMake(100, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil].size;
    _boundingRectWithSizeAttributedLabel.frame = CGRectMake(15, _boundingRectWithSizeLabel.bottom + 30, size1.width, size1.height);

 

来自:http://www.jianshu.com/p/ce26f05cd7cc