iOS7以下的系统可使用方法

//获得当前cell高度

CGRect frame = [self frame];

//文本赋值

self.introduction.text = text;

//设置label的最大行数

self.introduction.numberOfLines = 0;

CGSize size = CGSizeMake(300, 1000);

CGSize labelSize = [self.introduction.text sizeWithFont:self.introduction.font constrainedToSize:size lineBreakMode:NSLineBreakByClipping];

NSLog(@"w = %f", labelSize.width);

NSLog(@"h = %f", labelSize.height);

iOS7以上才支持的方法

//    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:self.introduction.text];

////    self.introduction.attributedText = attrStr;

//    NSRange range = NSMakeRange(0, attrStr.length);

//    NSDictionary *dic = [attrStr attributesAtIndex:0 effectiveRange:&range];

NSDictionary *dic = @{NSFontAttributeName: self.introduction.font};

CGSize labelSize1 = [self.introduction.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size;

NSLog(@"w = %f", labelSize1.width);

NSLog(@"h = %f", labelSize1.height);

self.introduction.frame = CGRectMake(self.introduction.frame.origin.x, self.introduction.frame.origin.y, labelSize1.width, labelSize1.height);

//计算出自适应的高度

frame.size.height = labelSize1.height+100;

self.frame = frame;

iOS6以上才支持的方法

NSAttributedString *str = [[NSAttributedString alloc] initWithString:self.introduction.text];

NSAttributedString *str1 = [[NSAttributedString alloc] initWithString:self.introduction.text attributes:@{NSFontAttributeName : self.introduction.font}];

CGSize size1 = [str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;

CGSize size2 = [str1 boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;

自适应label的高度的更多相关文章

  1. 基于jQuery自适应宽度跟高度可自定义焦点图

    基于jQuery自适应宽度跟高度可自定义焦点图.这是一款带左右箭头,缩略小图切换的jQuery相册代码.效果图如下: 在线预览   源码下载 实现的代码. html代码: <section cl ...

  2. ios webview自适应实际内容高度4种方法

    有的时候会碰见类似的苦逼需求, webview自适应实际内容高度 下面有四种方法供使用 方法1:获取webview中scrovllview的contentsize进行设置   1 2 3 4 5 6 ...

  3. iOS-动态计算Label的高度

    一. 要求 1.根据网络请求的回来的字符串内容,动态计算Label的高度. 二. 注意点 1. 要注意设置label 的 numberOfLines 为0; 2. MAXFLOAT 的作用. 设置高度 ...

  4. 如何设置自适应当前浏览器高度的div块

    嗯 就是下面这样 <!DOCTYPE html> <head> <title>adaptive this page size</title> <s ...

  5. 怎么可以让div自适应屏幕的高度?(已解决)

    主要解决问题的方法是用JS脚本. 先看布局, 一个div是首部,另一个div是主体,主体包含左侧菜单和右侧内容. 我想把主体div的高度自适应屏幕剩余区域,怎么做? 首先,获取可见区域的高度,docu ...

  6. 总结iframe高度自适应,自适应子页面高度

      在网上找了很多iframe的高度自适应,发现很多兼容性都不是很好,于是自己总结了一下. 页面html节点上要有 <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  7. ios 设置label的高度随着内容的变化而变化

    好吧 步骤1:创建label _GeRenJianJie = [[UILabel alloc]init]; 步骤2:设置label _GeRenJianJie.textColor = RGBAColo ...

  8. 根据文字计算出label的高度

    ios7.0之前用: [strtestsizeWithFont:ContentFontconstrainedToSize:CGSizeMake(ScreenWeight -20, 1000) line ...

  9. Winfrom固定Label宽度,根据文本动态改变Label的高度 z

    Label,要固定住宽度,然后根据文本的长度来动态改变高度,一开始去网上找解决方案,各种根据字体大小啊,字数啊来动态改变,但是效果却不怎么好.最后灵机一动,想起了偶尔用过一次的FlowLayoutPa ...

随机推荐

  1. 如何在腾讯云上开发一款O2O书签?

    版权声明:本文由潘佳宇原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/187 来源:腾云阁 https://www.qclo ...

  2. 图片左右滚动的js代码

    html代码 <div class="demo" id="demo" style="overflow:hidden; width:660px; ...

  3. IoC 之 2.2 IoC 容器基本原理(贰)

    2.2.1  IoC容器的概念 IoC容器就是具有依赖注入功能的容器,IoC容器负责实例化.定位.配置应用程序中的对象及建立这些对象间的依赖.应用程序无需直接在代码中new相关的对象,应用程序由IoC ...

  4. Technical notes fornight

    1.8.2016 Royal trumpeters heralded the beginning of the annual ceremony, as Norway's royal family an ...

  5. 浅谈线程池(中):独立线程池的作用及IO线程池

    原文地址:http://blog.zhaojie.me/2009/07/thread-pool-2-dedicate-pool-and-io-pool.html 在上一篇文章中,我们简单讨论了线程池的 ...

  6. js时间显示设置

    //对日期中部分小于10的数字前边添加0 function zero(s){ return s < 10 ? '0' + s: s; } var date=new Date(), year = ...

  7. android浮动搜索框

    android浮动搜索框的配置比较繁琐,需要配置好xml文件才能实现onSearchRequest()方法. 1.配置搜索的XML配置文件​,新建文件searchable.xml,保存在res/xml ...

  8. [redis] Jedis 与 ShardedJedis 设计

    Jedis设计 Jedis作为推荐的java语言redis客户端,其抽象封装为三部分: 对象池设计:Pool,JedisPool,GenericObjectPool,BasePoolableObjec ...

  9. Qt之QRadioButton

    简述 QRadioButton部件提供了一个带有文本标签的单选框(单选按钮). QRadioButton是一个可以切换选中(checked)或未选中(unchecked)状态的选项按钮.单选框通常呈现 ...

  10. hdu-----(1507)Uncle Tom's Inherited Land*(二分匹配)

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...