需求如下:
 
  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
 

最新文章

  1. java实现基于activeMQ的消息推送
  2. centos环境自动化批量安装软件脚本
  3. Mina、Netty、Twisted一起学(七):发布/订阅(Publish/Subscribe)
  4. 第三百四十九、五十天 how can I 坚持
  5. has-a关系——包含对象成员的类
  6. php学习之路
  7. jQuery Mobile发展新闻阅读器,适应iphone和android打电话
  8. map中结构体做关键字的注意事项
  9. VR全景智慧城市——商家的需求才是全景市场的核心竞争力
  10. JSSDK获取用户地理位置信息
  11. 练习html,css,js仿制百度首页
  12. Windows终端工具_MobaXterm
  13. How do I close a single buffer (out of many) in Vim?
  14. Http协议响应状态类别及说明
  15. Win10系列:JavaScript动画3
  16. 《算法》第四章部分程序 part 6
  17. XE6入门(二)项目中的文件
  18. unbuntu 安装及服务器配置
  19. toString方法的用法
  20. jQuery轮播图(手动点击轮播)

热门文章

  1. HashMap工作原理的介绍!
  2. twemproxy架构分析——剖析twemproxy代码前编
  3. 阐述php(五岁以下儿童) 注意事项和使用功能
  4. WPF制作Logo,很爽,今后在应用程序中加入Logo轻松,省事!
  5. Blend_技巧篇_导入PSD文件制作ToggleButton (Z)
  6. readline库的使用
  7. React学习(2)——action,reducer
  8. 淘宝平台进行数据的实时传输: TimeTunnel介绍
  9. Emgu-WPF学习使用-阈值化
  10. WPF 用代码增加路由事件的方法