uilabel 自适应
有时一个UILable的text内容是变化的,而且差异有很大,
- 需求上要求UILabel的大小高宽能够自适应text的内容。代码例子:
- myLable=[[UILabel alloc] initWithFrame:CGRectMake(0, 23, 175, 33)];
- [myLable setFont:[UIFont fontWithName:@"Helvetica" size:10.0]];
- [myLable setNumberOfLines:0];
- [myLable setBackgroundColor:[UIColor clearColor]];
- [myAdView addSubview:myLable];
- UIFont *font = [UIFont fontWithName:@"Helvetica" size:10.0];
- CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f)
- lineBreakMode:UILineBreakModeWordWrap];
- CGRect rect=myLable.frame;
- rect.size=size;
- [myLable setFrame:rect];
- [myLable setText:text];
- 核心的是
- CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f)
- lineBreakMode:UILineBreakModeWordWrap];
- 来预算text显示时宽高。
- 其中font是显示的字体,constrainedToSize是最大可接受的字符串宽高(例子中是宽175,高2000)
- lineBreakMode换行类型(UILineBreakModeWordWrap指的单词边界换行)
sizeWithFont:constrainedToSize:lineBreakMode:
Returns the size of the string if it were rendered with the specified constraints.
Parameters
- font
-
The font to use for computing the string size.
- size
-
The maximum acceptable size for the string. This value is used to calculate where line breaks and wrapping would occur.
- lineBreakMode
-
The line break options for computing the size of the string. For a list of possible values, see
NSLineBreakMode.
Return Value
The width and height of the resulting string’s bounding box. These values may be rounded up to the nearest whole number.
Discussion
You can use this method to obtain the layout metrics you need to draw a string in your user interface. This method does not actually draw the string or alter the receiver’s text in any way.
This method computes the metrics needed to draw the specified string. This method lays out the receiver’s text and attempts to make it fit the specified size using the specified font and line break options. During layout, the method may break the text onto multiple lines to make it fit better. If the receiver’s text does not completely fit in the specified size, it lays out as much of the text as possible and truncates it (for layout purposes only) according to the specified line break mode. It then returns the size of the resulting truncated string. If the height specified in the size parameter is less than a single line of text, this method may return a height value that is bigger than the one specified.
Availability
- Available in iOS 2.0 and later.
Declared In
UIStringDrawing.h
uilabel 自适应的更多相关文章
- UILabel 自适应宽高
#import <UIKit/UIKit.h> @interface UILabel (UILabel_LabelHeighAndWidth) + (CGFloat)getHeightBy ...
- UILabel自适应高、宽
根据Label和字体大小自适应高度 - (CGFloat)getHeightWithLabel:(UILabel *)label andFontSize:(CGFloat)size { label.n ...
- UILabel自适应高度,自动换行
CGRect rect; rect = self.labelInfo.frame; //UILabel高度自适应 rect.size.height = [self.labelInfo.text bou ...
- UILabel自适应高度
在网上看了一些,发现很多关于自适应高度的,不能用,就在下面写一种我常用的吧!保证可以直接粘贴复制到Xcode中运行. UILabel *label = [[UILabel alloc] init]; ...
- UILabel 自适应高度,宽度
mLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 10, 1)]; mLabel1.text = @"my label 1, ...
- iOS之UILabel自适应高度、宽度
下列两条自适应高度和宽度的自定义方法:
- UILabel自适应高度和自动换行
码: //初始化label UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)]; //设置自动行数与字符换行 [l ...
- iOS之UILabel自适应大小
//初始化一个label self.label=[[UILabel alloc] init]; //设置自动行数与字符换行 [self.label setNumberOfLines:0]; //给la ...
- UITableViewCell的高度与UILabel自适应
UITableViewCell内部只放了一个UILabel,Cell的高度随着UILabel内容的高度变化而变化,可重写UITableView的委托方法动态调整高度,还要设置UILabel.numbe ...
随机推荐
- 穷人的语义处理工具箱之中的一个:语义版Jaccard
/* 版权声明:能够随意转载,转载时请标明文章原始出处和作者信息 .*/ author: 张俊林 |为什么我们是ML界的穷人 假设对工业界里的机器学习(ML)从业者进行阶级划分的话,划线标准不是你用的 ...
- 不同classloader装载的类不能互相访问?
一,有两个术语,一个叫“定义类加载器”,一个叫“初始类加载器”. 比如有如下的类加载器结构: bootstrap ExtClassloader AppClassloader -自定 ...
- 纯css3实现的竖形二级导航
之前为大家分享了好多导航菜单.今天给大家带来一款纯css3实现的竖形二级导航.这款导航菜单可以是无限级.一起看下效果图: 在线预览 源码下载 实现的代码. html代码: <div styl ...
- C#使用SendMessage传递字符串
来源:http://www.cnblogs.com/sizzle/archive/2007/08/29/874796.html 正文: 在C#中使用SendMessage,原本以为很简单的事,却处处碰 ...
- C语言 · 约数个数
算法提高 约数个数 时间限制:1.0s 内存限制:512.0MB 输入一个正整数N,输出其约数的个数. 样例输入 12 样例输出 6 样例说明 12的约数包括:1,2,3,4,6,1 ...
- jQuery:(一)jQuery简介
一.jQuery简介jQuery由美国人John Resig于2006年创建jQuery是目前最流行的JavaScript程序库,它是对JavaScript对象和函数的封装. 二.jQuery的优势1 ...
- PostgreSQL 配置远程访问
配置远 程连接PostgreSQL数据库的步骤很简单,只需要修改data目录下的pg_hba.conf和postgresql.conf. pg_hba.conf:配置对数据库的访问权限, postgr ...
- JavaScript的gzip静态压缩方法记录
传统的JS压缩(删除注释,删除多余空格等)提供的压缩率有时还是不尽不意,幸亏现在的浏览器都支持压缩传输(通过设置http header的Content-Encoding=gzip),可以通过服务器的配 ...
- 股票指数kdj,sar,macd
http://blog.eastmoney.com/gulingqianketong2011/blog_120832611.html http://blog.sina.com.cn/s/blog_a3 ...
- Git神器sourceTreesetup,让你的git效率迅速提升
image.png 小编之前在简书和公众号发不过idea如何git clone,git add,创建分支,创建码云的操作idea和Webstorm上使用git和github,码云最近又找到一款git神 ...