根据文字计算出label的高度
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的高度的更多相关文章
- iOS 动态计算文本内容的高度
关于ios 下动态计算文本内容的高度,经过查阅和网上搜素,现在看到的有以下几种方法: 1. // 获取字符串的大小 ios6 - (CGSize)getStringRect_:(NSString* ...
- Label自适应高度的用法及设置倒角
UILabel *label = [[UILabel alloc] init]; //根据内容动态计算label的高度 label.text = @"Sent when the applic ...
- iOS-动态计算Label的高度
一. 要求 1.根据网络请求的回来的字符串内容,动态计算Label的高度. 二. 注意点 1. 要注意设置label 的 numberOfLines 为0; 2. MAXFLOAT 的作用. 设置高度 ...
- iOS中求出label中文字的行数和每一行的内容
今天遇到一个需求,需要计算label中文字的行数.想了好久也没想到好的解决方法,就在网上找了下.结果发现一篇文章是讲这个的.这部分代码不但能够求出一个label中文字行数,更厉害的是能够求出每一行的内 ...
- tableZen maxHeight 解决方案 如果数据条数小于N,不进行高度设置,超过N条,直接设置高度,解决原生iview Table 对于右侧固定列,不能计算出正确数值的解决方案
tableZen maxHeight 解决方案 如果数据条数小于N,不进行高度设置,超过N条,直接设置高度,解决原生iview Table 对于右侧固定列,不能计算出正确数值的解决方案 if (thi ...
- AutoLayout 根据文字、图片自动计算 UITableViewCell 高度
原文网址: http://lvwenhan.com/ios/449.html 此系列文章代码仓库在 https://github.com/johnlui/AutoLayout ,有不明白的地方可以参考 ...
- iOS开发之计算动态cell的高度并缓存
项目中有个类似微博那样的动态cell,文字和图片的多少都不是确定的 刚开始使用autolayout,结果很多问题,最后我发现了一个框架 FDTemplateLayoutCell 写的很好,自动布局ce ...
- iOS计算字符串的宽度高度
OC开发中会遇到根据字符串和字体大小来算计算出字符串所占的宽高->> 封装方法如下: #import <Foundation/Foundation.h> #import < ...
- 自适应label的高度
iOS7以下的系统可使用方法 //获得当前cell高度 CGRect frame = [self frame]; //文本赋值 self.introduction.text = text; //设置l ...
随机推荐
- JSP第七篇【简单标签、应用、DynamicAttribute接口】
为什么要用到简单标签? 上一篇博客中我已经讲解了传统标签,想要开发自定义标签,大多数情况下都要重写doStartTag(),doAfterBody()和doEndTag()方法,并且还要知道SKIP_ ...
- svn清理失败且乱码 问题解决
由于昨天在网络不好的状态下频繁尝试svn更新,导致今天svn更新时出现:清理失败且乱码的情况如下: 以下是解决方案:1.下载sqlite3.exe ,地址为:http://download.csdn. ...
- Python的json and pickle序列化
json序列化和json反序列化 #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = '人生入戏' import json a = ...
- 出现java.sql.SQLException: No suitable driver的几种解决办法
今天在用C3p0连接MySql数据库 这个时候出现了上面的错误, 一般来说这种错误有3个方向解决 一般都是db的配置的问题 ,一定要小心DB 的配置 八月 19, 2017 8:30:46 下午 co ...
- Linux Ubuntu从零开始部署web环境及项目 -----tomcat+jdk+mysql (二)
上一篇介绍如何在linux系统下搭建ssh环境 这篇开始将如何搭建web服务器 1,下载文件 在官网下载好 tomcat.jdk.mysql的linux压缩包 后缀名为.tar.gz 并通过xftp上 ...
- windows 结束进程的详细过程
windows上如何结束进程的详细过程,下面附详细,图文说明 在cmd下,输入 netstat -ano|findstr 8080 //说明:查看占用8080端口的进程 在cmd下, ...
- 9月24日noip模拟赛解题报告
1.校门外的树(tree.c/cpp/pas 128M,1s) Description LSGJ扩建了,于是校门外有了一条长为L的路.路上种了一排的树,每相邻两棵树之间的距离为1,我们可以把马路看成一 ...
- Linux学习——shell编程之正则表达式和字符处理命令
shell编程之正则表达式 一 正则表达式 1 什么是正则表达式 正则表达式用于描述字符排列和匹配模式的一种语法规则.它主要用于字符串的模式分隔.匹配.查找及替换操作. 2 shell编程之正则表达式 ...
- HDFS源码分析之NameNode(1)————启动过程
源码:2.8.0 入口类:org.apache.hadoop.hdfs.server.namenode.NameNode main方法会调用createNameNode 创建 NameNode 实例, ...
- 数据分析前戏:ipython使用技巧(上)
不一定非得使用Jupyter Notebook,试试ipython命令行 安装 ipython 我只试过Windows 10环境下的. 1.安装python安装包之后,应该就有ipython了. 2. ...