需求如下:
 
  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. CodeBlocks提供了预编译的WxWidgets模块,并预置TDM

    Miscellaneous For Windows, we also provide the pre-compiled wxWidgets, version 2.8.12 used to compil ...

  2. 链接hdf5库出现错误的解决办法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 在链接hdf5库出现一些链接错误: error LNK2001: 无法解析的外部符号 _H5T_NATIVE_DOUB ...

  3. HDU 1671 (字典树统计是否有前缀)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...

  4. March 29th, 2015, Thread Name is odd by increasing 1

    public class Fabric extends Thread{ public static void main(String args[]){ Thread t = new Thread(ne ...

  5. 我眼中的robot framework

    由于近期公司需要,需要一个测试框架对于公司的服务做自动化测试. 由于服务的复杂性,人工测试的方式越来越复杂,体现在以下方面: 1.人工测试步骤复杂,容易出错.服务的复杂性会使人工测试的准备工作,测试条 ...

  6. Windows DPI Awareness for WPF

    原文 Windows DPI Awareness for WPF 对于 WPF 程序,要控制程序的 DPI 感知程度,可在 App.manifest 中添加如下代码. 本文知识已经陈旧,你可以阅读这两 ...

  7. 双目相机标定以及立体测距原理及OpenCV实现

    单目相机标定的目标是获取相机的内参和外参,内参(1/dx,1/dy,Cx,Cy,f)表征了相机的内部结构参数,外参是相机的旋转矩阵R和平移向量t.内参中dx和dy是相机单个感光单元芯片的长度和宽度,是 ...

  8. BS学习概述

    从最初的牛腩新闻公布系统,到如今的JS,回想一下,自己的BS也算是学了大半,可是有时候想起来还是总是有一种不踏实的感觉,一是由于从开学到如今赶上了三级考试,自考.软考,导致BS学习时间被大大压缩了,代 ...

  9. Android获取百度音乐下载音乐和歌词下载链接

    首先,你必须通过以下连接下载歌曲: http://box.zhangmen.baidu.com/x?op=12&count=1&title={title}$${author}$$$$ ...

  10. Android sendToTarget

    在使用message进行handler的数据交互的时候不可避免的会使用到message作为数据的载体,可是在使用message的时候有人会直接new一个message,有人会使用handler.obt ...