需求如下:
 
  1. 需要显示2行文字,宽度为 SCREEN_Width - 40 高度为两行文本的自适应高度
  2. 需要在此UILabel 下面添加imageView , 因此UIlabel 的高度需要准确,不允许有空白行出现
  3. UILabel 内的文本较多,需要加省略符:。。。
 
图片效果如下:
 
 
 
实现方式:
 
第一种:通过 sizeToFit 方法来实现
 
Coding:
 
UILabel *ddlabel = [[UILabel alloc] init];
    [ddlabel setText:@"根据文字高度进行适应根据文字高度进行自适应根据文适应根据文字高度进行自适应根据文自适应根据文字高度进行自适应根据文字高度进行自适应根据文字高度进行自适应根据文字高度进行自适应"];
    ddlabel.numberOfLines = 2;
    ddlabel.font = [UIFontsystemFontOfSize:24];
    ddlabel.frame = CGRectMake(20, CGRectGetMaxY(image1.frame),SCREEN_Width - 40,rect.size.height);
    [ddlabel sizeToFit];
    
 
第二种:通过 sizeToFit 方法来实现:
 
    1. 获取text属性的文本内容
    2. 重新定义宽度和高度
    3. 设置换行模式
    4. 计算CGRect并重新设置UILabel的frame。
    5. CGRect rect = [label.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: label.font} context:nil];
 
 
Coding:
 
    UILabel *ddlabel = [[UILabelalloc] init];
    [ddlabel setText:@"根据文字高度进行适应根据文字高度进行自适应根据文适应根据文字高度进行自适应根据文自适应根据文字高度进行自适应根据文字高度进行自适应根据文字高度进行自适应根据文字高度进行自适应"];
    ddlabel.numberOfLines = 2;
    ddlabel.font = [UIFontsystemFontOfSize:24];

    CGRect rect = [ddlabel.textboundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeadingattributes:@{NSFontAttributeName: ddlabel.font}context:nil];
   
    ddlabel.frame = CGRectMake(20, CGRectGetMaxY(image1.frame),SCREEN_Width - 40,rect.size.height);

 
 
效果图如下:
 
 
咦,发现问题没有?第二种方式算出来的竟然有空行,而且这是大家最常用的方法,我们把换行设为0看一下效果
 
ddlabel.numberOfLines = ;
 
效果图如下:
 
 
第二种方式计算的并不是 两个文本的高度,而是所有文本的高度,如果通过想得到效果图的效果,那就设置最高范围:
 
titleLabRealHeight = rect.size.height
ddlabel.frame = CGRectMake(20, CGRectGetMaxY(image1.frame),SCREEN_Width - 40, titleLabRealHeight>46?46:titleLabRealHeight + 4);
 
这种方法是已知两行文字的高度为 46,来设置文本框的高度。
 
Ps :通过对比发现还是第一种方法好,希望大家尽量使用 sizeToFit方法。
 
 
但是!!如果  :UILabel sizeToFit doesn't work with autolayout ios6 ,这时怎么办?
 
请按照这个顺序设置约束和方法:

 
To make your label automatically resize height you need to do following:
 
  1. Set layout constrains for label
  2. Set height constraint with low priority. It should be lower than ContentCompressionResistancePriority
  3. Set numberOfLines = 0
  4. Set ContentHuggingPriority higher than label's height priority
  5. Set preferredMaxLayoutWidth for label. That value is used by label to calculate its height
 
 
For example:
 
self.descriptionLabel = [[UILabel alloc] init];
self.descriptionLabel.numberOfLines = 0;
self.descriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.descriptionLabel.preferredMaxLayoutWidth = 200;

[self.descriptionLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[self.descriptionLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[self.descriptionLabel

setTranslatesAutoresizingMaskIntoConstraints

:NO];
[self addSubview:self.descriptionLabel];

NSArray* constrs = [NSLayoutConstraint constraintsWithVisualFormat:@"|-8-[descriptionLabel_]-8-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(descriptionLabel_)];
[self addConstraints:constrs];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[descriptionLabel_]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(descriptionLabel_)]];
[self.descriptionLabel addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[descriptionLabel_(220@300)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(descriptionLabel_)]];

 
 
 
—— author —— : Levi.duan
 

UILabel实现自适应宽高需要注意的地方的更多相关文章

  1. UILabel实现自适应宽高需要注意的地方(三)

        一.需求图如下所示    UILabel 的高度自适应 UILabel中的段落间距可设置   图片效果如下:   调整段落适应长宽高方式:         需求:   保证"游戏玩法 ...

  2. UILabel实现自适应宽高需要注意的地方(二)

    需求图如下所示   UILabel "上期"   距离屏幕最左边 有35px UILabel "下期"   距离屏幕最右边 有35px 进行中文字在UIlabe ...

  3. 小程 序swiper自适应宽高

    https://blog.csdn.net/qq_31604363/article/details/73715944 小程 序swiper自适应宽高 小程 序swiper自适应宽高

  4. OpenGL ES学习笔记(二)——平滑着色、自适应宽高及三维图像生成

    首先申明下,本文为笔者学习<OpenGL ES应用开发实践指南(Android卷)>的笔记,涉及的代码均出自原书,如有需要,请到原书指定源码地址下载. <Android学习笔记--O ...

  5. iOS - web自适应宽高(预设置的大小)

    //web自适应宽高 -(void)webViewDidFinishLoad:(UIWebView *)webView { NSLog(@"wessd"); [ webView s ...

  6. iview Carousel 轮播图自适应宽高;iview 轮播图 图片重叠问题;iview tabs 高度互相影响问题;vue this问题;

    最终效果图: 一.轮播图中图片自适应宽高:  <Carousel loop v-bind:height="imgHeight+'px'" v-model="caro ...

  7. UILabel 自适应宽高

    #import <UIKit/UIKit.h> @interface UILabel (UILabel_LabelHeighAndWidth) + (CGFloat)getHeightBy ...

  8. js和php计算图片自适应宽高算法实现

    js Code: <script> $width = $(imgobj).width(); //图原始宽 $newheight = $(imgobj).height(); //图原始高 $ ...

  9. css background-image 自适应宽高——转载

    就是这么简单的一句话,设置背景图,并让它100%的适应导航栏宽高,并设置不重复,大小100%就OK了 .zjhn-nav li.active a{ background-image:url(../im ...

随机推荐

  1. Compile Graphics Magick, Boost, Botan and QT with MinGW64 under Windows 7 64

    Compile Graphics Magick, Boost, Botan and QT with MinGW64 under Windows 7 64 Sun, 01/01/2012 - 15:43 ...

  2. Dojo第一节:学会使用firebug对js,Dojo进行调适

    内容概要: 学会使用firebug的基本功能 1. 简介:Firebug是Firefox的一个插件,用来对js代码进行调适的工具. (官方废话:Firebug是firefox下的一个插件,可以调试全部 ...

  3. C#中的反射总结

      = 导航   顶部 什么是反射 为什么要使用反射 C#反射相关的命名空间 通过反射创建类型的实例 通过反射调用类的方法 查看类中成员 查看类的构造函数 查看类中属性 查看类中字段 反射实现的接口 ...

  4. 查看系统中安装了那些dotnet core 的SDK和运行时的命令

    原文:查看系统中安装了那些dotnet core 的SDK和运行时的命令 1.查看SDK dotnet --list-sdks 2.查看运行时 dotnet --list-runtimes 效果如下图 ...

  5. Dx bad class file magic (cafebabe) or version (0033.0000) ant打包遇到问题2

    在进行ant进行打包时会发现下面的提示话语言 后来在网上搜索答案,问题得以解决,下面是传送门 门:http://blog.k-res.net/archives/1501.html 里面提到问题的原因是 ...

  6. 【书单】matlab 科学计算、数值分析以及数学物理问题

    1. 数学计算 MATLAB数值计算 MATLAB之父 : 编程实践 2. 数学物理问题 高等应用数学问题的MATLAB求解(第3版)(豆瓣评价极好) 3. 模式识别

  7. 微信小程序支付结果 c#后台回调

    又为大家带来简单的c#后台支付结果回调方法,首先还是要去微信官网下载模板(WxPayAPI),将模板(WxPayAPI)添加到服务器上,然后在打开WxPayAPI项目中的example文件下的 Nat ...

  8. malloc()与calloc差异

    Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slig ...

  9. AngularJS $http和$.ajax

    $http请求 $http请求返回之后,给前台绑定数据赋值,会自动更新数据 ajax请求 $.ajax请求返回之后,给前台绑定数据赋值,不会自动更新数据,需要用$scope.$apply手动刷新 ap ...

  10. jquery 访问cookie

    <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...