iOS-自适应总结
lijingwei_
8年前
<p><strong>UIView方法之SizeToFit</strong></p> <p>作用: 计算出最优size, 并且改变UIView的size</p> <p><strong>Demo1: 高度不变,宽度随文本大小变化而变化</strong></p> <p>设置字号为13,使用SizeToFit自适应结果为</p> <pre> <code class="language-objectivec">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));</code></pre> <p>UILabel打印结果为:</p> <p>frame = {{20, 100}, {307, 16}}</p> <pre> <code class="language-objectivec">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));</code></pre> <p>UIButton打印结果为:</p> <p><strong>frame = {{20, 100}, {540, 28}}</strong></p> <pre> <code class="language-objectivec">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));</code></pre> <p>UITextField打印结果为:</p> <p><strong>frame = {{20, 100}, {544, 16}}</strong></p> <p>此时三个控件都是单行模式下,高度不变,宽度随文本大小变化而变化.</p> <p>但UILabel是有设置多行功能的,所以UILabel和UIButton均可设置多行,请看Demo2</p> <p><strong>Demo2: 宽度不变,高度随文本大小变化而变化</strong></p> <p>设置宽度为100,UILabel的numberOfLines大于1行,假如为3行</p> <p>若自适应后的宽度小于100,则宽度小于100,高为一行</p> <p>若是自适应后的宽度大于100,则宽度为100,若高度大于3行,则高度为3行,剩余内容不显示,若高度小于三行,则高度为计算出的高度</p> <pre> <code class="language-objectivec">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));</code></pre> <p>UIlabel打印结果为:</p> <p><strong>frame = {{20, 100}, {97, 47}}</strong></p> <p><strong>Demo3: 宽度不变,高度随文本大小变化而变化</strong></p> <p>设置宽度为100,UILabel的numberOfLines为0行</p> <p>若自适应后的宽度小于100,则宽度小于100,高为一行</p> <p>若是自适应后的宽度大于100,则宽度为100,高度为自适应后的高度</p> <p>注意:高度自适应时,宽度不可设置为0,若为0,则变成了单行宽度自适应</p> <p>但刚刚自适应算出来的坐标,上下左右均紧贴着Label的边,不美观,可做如下修饰</p> <pre> <code class="language-objectivec">label.width += 10; label.height += 10;</code></pre> <p style="text-align:center"><img src="https://simg.open-open.com/show/6ae85baf5919a657dad9f88be0dfd8cb.png"></p> <p>UIView方法之sizeThatFits</p> <p>作用: 计算出最优size, 但是不会改变UIView的size</p> <p>用法: 将sizeThatFits的宽高设置较大点,会在这个范围内自动计算出最匹配宽高</p> <p>若计算的宽度小于设置的宽度,则以计算出来的高度为准</p> <p>若计算的宽度大于设置的宽度,则以设置的宽度去进行高度自适应</p> <p>注意:若要自适应,要重设Label坐标</p> <pre> <code class="language-objectivec">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));</code></pre> <p>UIlabel打印结果为:(因为有换行,所以计算出)</p> <p><strong>frame = {80, 62.5}</strong></p> <p><strong>NSString方法之boundingRectWithSize</strong></p> <pre> <code class="language-objectivec">_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);</code></pre> <p><strong>NSAttributedString方法之boundingRectWithSize</strong></p> <pre> <code class="language-objectivec">_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);</code></pre> <p> </p> <p>来自:http://www.jianshu.com/p/ce26f05cd7cc</p> <p> </p>