iOS 根据文字字数动态确定Label宽高
iOS7中用以下方法
- (CGSize)sizeWithAttributes:(NSDictionary *)attrs;
替代过时的iOS6中的- (CGSize)sizeWithFont:(UIFont *)font 方法
// iOS7_API_根据文字 字数动态确定Label宽高
// 设置Label的字体 HelveticaNeue Courier
UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:24.0f];
_nameLabel.font = fnt;
// 根据字体得到NSString的尺寸
CGSize size = [_nameLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
// 名字的H
CGFloat nameH = size.height;
// 名字的W
CGFloat nameW = size.width;
_nameLabel.frame = CGRectMake(0, 0, nameW,nameH);
iOS7中用以下方法boundingRectWithSize:options:attributes:context:替代过时的iOS6中的sizeWithFont:constrainedToSize:lineBreakMode:方法
// 4,根据正文内容多少,动态确定正文content的frame
// 宽度W
CGFloat contentW = self.bounds.size.width - _content.frame.origin.x - kMargin;
// label的字体 HelveticaNeue Courier
UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:18.0f];
_content.font = fnt;
_content.numberOfLines = 0;
_content.lineBreakMode = NSLineBreakByWordWrapping;
// iOS7中用以下方法替代过时的iOS6中的sizeWithFont:constrainedToSize:lineBreakMode:方法
CGRect tmpRect = [_content.text boundingRectWithSize:CGSizeMake(contentW, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil] context:nil];
// 高度H
CGFloat contentH = tmpRect.size.height;
NSLog(@"调整后的显示宽度:%f,显示高度:%f"contentW,contentH);
_content.frame = CGRectMake(0, 0, contentW,contentH);
附:API文档参考
boundingRectWithSize:options:attributes:context:
Calculates and returns the bounding rect for the receiver drawn using the given options and display characteristics, within the specified rectangle in the current graphics context.
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context
Parameters
size
The size of the rectangle to draw in.
options
String drawing options.
attributes
A dictionary of text attributes to be applied to the string. These are the same attributes that can be applied to anNSAttributedString object, but in the case ofNSString objects, the attributes apply to the entire string, rather than ranges within the string.
context
The string drawing context to use for the receiver, specifying minimum scale factor and tracking adjustments.
Return Value
The bounding rect for the receiver drawn using the given options and display characteristics. The rect origin returned from this method is the first glyph origin.
Discussion
To correctly draw and size multi-line text, pass NSStringDrawingUsesLineFragmentOrigin in the options parameter.
This method returns fractional sizes (in the size component of the returnedCGRect); to use a returned size to size views, you must raise its value to the nearest higher integer using theceil function.
This method returns the actual bounds of the glyphs in the string. Some of the glyphs (spaces, for example) are allowed to overlap the layout constraints specified by the size passed in, so in some cases the width value of the size component of the returned CGRect can exceed the width value of the size parameter.
Availability
- Available in iOS 7.0 and later.
See Also
- – drawInRect:withAttributes:
Declared In
NSStringDrawing.h
iOS 根据文字字数动态确定Label宽高的更多相关文章
- iOS根据文字字数动态确定Label宽高
我们有时候在写项目的时候,会碰到,意见反馈,还有其他地方,讲座活动细则等需要大篇展示的文本, 因为每次服务器返回的内容大小不一,所以需要动态的调整label的宽高: 在ios 6 的时候可以: -(v ...
- iOS_根据文字字数动态确定Label宽高
iOS7中用以下方法 CGSize 替代过时的iOS6中的- (CGSize)sizeWithFont:(UIFont *)font 方法 // iOS7_API_根据文字 字数动态确定Label宽高 ...
- 根据字符长度动态确定UIlabel宽高
iOS7中用以下方法 - (CGSize)sizeWithAttributes:(NSDictionary *)attrs; 替代过时的iOS6中的- (CGSize)sizeWithFont:(UI ...
- 动态布局--动态修改RelativeLayout宽高的方法
本文实例讲述了Android编程动态修改RelativeLayout宽高的方法.分享给大家供大家参考,具体如下: 我们经常会动态修改RelativeLayout的宽高,这样的代码,比较简单,就是修改R ...
- 写个js动态调整图片宽高 (原创)
<body style="TEXT-ALIGN: center;"> <div id="testID" style="backgro ...
- iOS学习-压缩图片(改变图片的宽高)
压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高. 传入想要修改的图片,以及新的尺寸 -(UIImage*)imageWithImage:(UIImage*)image ...
- 微信小程序之动态获取元素宽高
我以前一直以为微信小程序不能动态获取view元素的宽高.但是自从看到: wx.createSelectorQuery() 这个api接口,以前的某些问题就能得到解决了... 那么,这个api接口怎么用 ...
- vue 动态获取div宽高有时候为0的情况
项目背景: 需要使用echarts进行图表展示.由于div宽高是不固定的,因此需要先获取父级的宽高再把值赋予到图表的div中. 需要使用 this.$nextTick(() => { }) ...
- 【iOS】获取视图的中心和宽高
示例代码: NSLog(@"%f, %f", self.view.center.x, self.view.center.y); NSLog(@"%f, %f", ...
随机推荐
- sudo -i和sudo -s
sudo -i,加载用户变量,并跳转到目标用户home目录:sudo -s,不加载用户变量,不跳转目录: sudo : 暂时切换到超级用户模式以执行超级用户权限,提示输入密码时该密码为当前用户的密码, ...
- 分布式任务调度平台XXL-Job搭建
下载: https://github.com/xuxueli/xxl-job 下载 然后倒入到自己的工程里面 引入后: 导入数据:跑一边 导入: 修改: Window -->show view- ...
- centos6.3 安装python2.7.3
现在比较流行python2.7版本,centos6.3的默认版本是2.6.6,所以需要安装下2.7版本 1.下载安装python2.7 #wget http://www.python.org/ftp/ ...
- kettle 设置变量
以下只是本人在使用过程中一些经验,可能有误解不对的地方,希望大家指正. 这个控件可以在job中调用,也可以在transformation中使用.下面将分别说明在两个不同任务中调用时的使用方法和需要注意 ...
- WEB攻击之 CSRF 攻击及防御策略
介绍 是一种挟制用户在当前已登录的Web应用程序上执行非本意的操作的攻击方法. 释义: 跨站请求攻击,简单地说,是攻击者通过一些技术手段欺骗用户的浏览器去访问一个自己曾经认证过的网站并运行一些操作(如 ...
- CodeForces-329C(div1):Graph Reconstruction(随机&构造)
I have an undirected graph consisting of n nodes, numbered 1 through n. Each node has at most two in ...
- CodeForcesdiv1:995C - Leaving the Bar(随机算法+贪心)
For a vector →v=(x,y)v→=(x,y), define |v|=√x2+y2|v|=x2+y2. Allen had a bit too much to drink at the ...
- MySQL当月汇总 及负毛利汇总_20161027
#当月汇总 及负毛利汇总 SELECT e.ID,e.city AS 城市 ,f.当月销售总额,f.当月成本总额,f.当月毛利总额,f.当月优惠券总额,f.当月赠品总额,f.当月毛利总额-f.当月优惠 ...
- 【Lintcode】382.Triangle Count
题目: Given an array of integers, how many three numbers can be found in the array, so that we can bui ...
- ScikitLearn 学习器类型
源自在线课程的学习:http://www.studyai.com/course/detail/d086826e9be84b818f9c54893633663d