ios7.0之前用:

[strtestsizeWithFont:ContentFontconstrainedToSize:CGSizeMake(ScreenWeight -20, 1000) lineBreakMode:NSLineBreakByWordWrapping];

ios7之后

CGRect frame = [strtestboundingRectWithSize:CGSizeMake(220,1000) options:NSStringDrawingUsesLineFragmentOriginattributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:10]}context:nil];

attributes参数有:

NSFontAttributeName // UIFont, default Helvetica(Neue) 12

NSParagraphStyleAttributeName // NSParagraphStyle, default defaultParagraphStyle

NSForegroundColorAttributeName // UIColor, default blackColor

NSBackgroundColorAttributeName // UIColor, default nil: no background

NSLigatureAttributeName // NSNumber containing integer, default 1: default ligatures, 0: no ligatures

NSKernAttributeName // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled.

NSStrikethroughStyleAttributeName ; // NSNumber containing integer, default 0: no strikethrough

NSUnderlineStyleAttributeName // NSNumber containing integer, default 0: no underline

NSStrokeColorAttributeName // UIColor, default nil: same as foreground color

NSStrokeWidthAttributeName // NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)

NSShadowAttributeName ; // NSShadow, default nil: no shadow

NSTextEffectAttributeName // NSString, default nil: no text effect

NSAttachmentAttributeName // NSTextAttachment, default nil

NSLinkAttributeName // NSURL (preferred) or NSString

NSBaselineOffsetAttributeName // NSNumber containing floating point value, in points; offset from baseline, default 0

NSUnderlineColorAttributeName // UIColor, default nil: same as foreground color

NSStrikethroughColorAttributeName // UIColor, default nil: same as foreground color

NSObliquenessAttributeName // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew

NSExpansionAttributeName // NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion

建立label的时候注意:

① label的font,必须和计算时用的font一样

② label的numberOfLines要设置为零

③ label的宽度设置成计算时设置的宽度一致

注意:在UITextView中用这个计算,得到的高度偏小,主要是UITextView中又默认的边距,需要自己根据具体情况进行相应的调整,特别是在文字比较多的情况下

根据文字计算出label的高度的更多相关文章

  1. iOS 动态计算文本内容的高度

    关于ios 下动态计算文本内容的高度,经过查阅和网上搜素,现在看到的有以下几种方法: 1. //  获取字符串的大小  ios6 - (CGSize)getStringRect_:(NSString* ...

  2. Label自适应高度的用法及设置倒角

    UILabel *label = [[UILabel alloc] init]; //根据内容动态计算label的高度 label.text = @"Sent when the applic ...

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

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

  4. iOS中求出label中文字的行数和每一行的内容

    今天遇到一个需求,需要计算label中文字的行数.想了好久也没想到好的解决方法,就在网上找了下.结果发现一篇文章是讲这个的.这部分代码不但能够求出一个label中文字行数,更厉害的是能够求出每一行的内 ...

  5. tableZen maxHeight 解决方案 如果数据条数小于N,不进行高度设置,超过N条,直接设置高度,解决原生iview Table 对于右侧固定列,不能计算出正确数值的解决方案

    tableZen maxHeight 解决方案 如果数据条数小于N,不进行高度设置,超过N条,直接设置高度,解决原生iview Table 对于右侧固定列,不能计算出正确数值的解决方案 if (thi ...

  6. AutoLayout 根据文字、图片自动计算 UITableViewCell 高度

    原文网址: http://lvwenhan.com/ios/449.html 此系列文章代码仓库在 https://github.com/johnlui/AutoLayout ,有不明白的地方可以参考 ...

  7. iOS开发之计算动态cell的高度并缓存

    项目中有个类似微博那样的动态cell,文字和图片的多少都不是确定的 刚开始使用autolayout,结果很多问题,最后我发现了一个框架 FDTemplateLayoutCell 写的很好,自动布局ce ...

  8. iOS计算字符串的宽度高度

    OC开发中会遇到根据字符串和字体大小来算计算出字符串所占的宽高->> 封装方法如下: #import <Foundation/Foundation.h> #import < ...

  9. 自适应label的高度

    iOS7以下的系统可使用方法 //获得当前cell高度 CGRect frame = [self frame]; //文本赋值 self.introduction.text = text; //设置l ...

随机推荐

  1. Intellij idea插入表数据【在UI界面插入出错】

    使用Intellij idea向数据库插入表数据的时候,如果该表是联合主键的,那么不能使用UI界面来进行插入-- 必须通过SQL语句才能插入-- 至于为什么?我也不知道-.搞了大半天--想省时间不写S ...

  2. 01快速入门-04-Map、Set和iterable(ES6)

    1.Map 我们知道,在JS中其实对象的方式就跟Java中的Map极为相似,即键值对的方式.JS中,key必须是字符串,实际上Number等值作为key也是合理的,所以为了解决这个问题,在最新的ES6 ...

  3. Flask-WTF CSRF 保护P3

    使用FlaskForm处理请求的任何视图函数都已经获得了CSRF保护.如果有些视图函数还在使用FlaskForm或AJAX请求,请尽快使用FlaskForm提供的CSRF扩展来保护它们. 导入 想要为 ...

  4. hadoop2.0的数据副本存放策略

    在hadoop2.0中,datanode数据副本存放磁盘选择策略有两种方式: 第一种是沿用hadoop1.0的磁盘目录轮询方式,实现类:RoundRobinVolumeChoosingPolicy.j ...

  5. [python学习笔记] 开篇

    今天开始学习markdown,立贴为证. 平均一天一小时.两个月先学完基础. 所有的帖子只为了学习记录,有不对的请提出,轻吐槽

  6. 我的Spring学习记录(三)

    学习了AOP之后就可以应用一下了,所以这次我们了解一下Spring的声明式事务. 事务在我们的很多方面都可以体现,就拿我们平时的买卖活动,或者是银行的转账来说,这些活动要么是成功,要么是失败,比如:张 ...

  7. 关于数据库中datareader的用法

    1.C#中提供的DataReader可以从数据库中每次提取一条数据. using System; using System.Collections.Generic; using System.Comp ...

  8. WaitAll 和 WhenAll 的使用及区别

    用过.net 异步编程的同学都知道,比以前的多线程编程实现起来真的方便很多,今天把WaitAll和WhenAll这两种编程方式回顾总结一下(当然WaitAny.WhenAny是一样的操作) 1:Wai ...

  9. 第4章 同步控制 Synchronization ----死锁(DeadLock)

    Jeffrey Richter 在他所主持的 Win32 Q&A 专栏(Microsoft Systems Journal,1996/07)中曾经提到过,Windows NT 和 Window ...

  10. (一) .net core 2.0 初体验

    1..net core 2.0环境 .net core 下载地址:https://www.microsoft.com/net/core#windowscmd 问题一:提示[Failed to load ...