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宽高的更多相关文章

  1. iOS根据文字字数动态确定Label宽高

    我们有时候在写项目的时候,会碰到,意见反馈,还有其他地方,讲座活动细则等需要大篇展示的文本, 因为每次服务器返回的内容大小不一,所以需要动态的调整label的宽高: 在ios 6 的时候可以: -(v ...

  2. iOS_根据文字字数动态确定Label宽高

    iOS7中用以下方法 CGSize 替代过时的iOS6中的- (CGSize)sizeWithFont:(UIFont *)font 方法 // iOS7_API_根据文字 字数动态确定Label宽高 ...

  3. 根据字符长度动态确定UIlabel宽高

    iOS7中用以下方法 - (CGSize)sizeWithAttributes:(NSDictionary *)attrs; 替代过时的iOS6中的- (CGSize)sizeWithFont:(UI ...

  4. 动态布局--动态修改RelativeLayout宽高的方法

    本文实例讲述了Android编程动态修改RelativeLayout宽高的方法.分享给大家供大家参考,具体如下: 我们经常会动态修改RelativeLayout的宽高,这样的代码,比较简单,就是修改R ...

  5. 写个js动态调整图片宽高 (原创)

    <body style="TEXT-ALIGN: center;"> <div id="testID" style="backgro ...

  6. iOS学习-压缩图片(改变图片的宽高)

    压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高. 传入想要修改的图片,以及新的尺寸 -(UIImage*)imageWithImage:(UIImage*)image ...

  7. 微信小程序之动态获取元素宽高

    我以前一直以为微信小程序不能动态获取view元素的宽高.但是自从看到: wx.createSelectorQuery() 这个api接口,以前的某些问题就能得到解决了... 那么,这个api接口怎么用 ...

  8. vue 动态获取div宽高有时候为0的情况

    项目背景: 需要使用echarts进行图表展示.由于div宽高是不固定的,因此需要先获取父级的宽高再把值赋予到图表的div中. 需要使用 this.$nextTick(() => {    }) ...

  9. 【iOS】获取视图的中心和宽高

    示例代码: NSLog(@"%f, %f", self.view.center.x, self.view.center.y); NSLog(@"%f, %f", ...

随机推荐

  1. Builder 模式初探

    Builder 模式是一步一步创建一个复杂对象的创建型模式,它允许用户在不知道内部构建细节的情况下,可以更精细的控制对象的构造流程.该模式是为了将构建复杂对象的过程和它的部件解耦,使得构建过程和部件的 ...

  2. mysql优化之 EXPLAIN(一)

    数据库优化最常用的命令就是用explain查看一下写的sql是否用到了索引: 如: (root@localhost) [akapp]>explain select * from sc_activ ...

  3. Android Weekly Notes Issue #320

    Android Weekly Issue #320 July 29th, 2018 Android Weekly Issue #320 本期内容包括: Firebase的MLKit; 关于写Andro ...

  4. event loop笔记

    注意四个对象: 主进程 执行栈 异步进程 任务队列 1.主进程读取js代码,形成对应的堆和执行栈(此时在同步环境) 2.当遇见异步任务,转交给异步进程进行处理 3.异步任务完成,将其推入任务队列 4. ...

  5. ActiveMQ持久化机制

    用户注册成功后发短信提醒 同步http 异步mq JMS中两种通讯模式: 发布订阅   一对多  topic   去过消费者集群的话 都会消费 消息队列   点对点 queue  去过消费者集群的话 ...

  6. html5 手写的canvas实现

    试用支持canvas的浏览器,无JS依赖,运用新的HTML5技术DrawBoard.renderDrawer('myHandWrite',{  penColor:'#FF0000',  penWidt ...

  7. BZOJ 1208 [HNOI2004]宠物收养所:Splay(伸展树)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1208 题意: 有一个宠物收养所,在接下来一段时间内会陆续有一些宠物进到店里,或是一些人来领 ...

  8. AtCoder Grand Contest #026 B - rng_10s

    Time Limit: 2 sec / Memory Limit: 1024 MB Score : 600600 points Problem Statement Ringo Mart, a conv ...

  9. 京东ie6中轮播模块小图出现在大图上

    请大家给个评论,给个支持!呵呵 本人最新一套模版小清新童装母婴日韩风全屏轮播(上线风暴),在审核时审核失败,报的是“ie6中全屏海报轮播是小图出现在大图中间的兼容性错误” 而本人本机出现的是小图基本上 ...

  10. 有趣的Javascript:只需一个JS让万恶的IE5、IE6、IE7、IE8全都支持H5原生Canvas绘图(有演示demo)

    该demo支持IE5以上任意内核的浏览器 查看演示demo:支持IE5以上版本的浏览器Canvas绘图demo 补充:chats.js和echarts等图表库也可以使用本方法兼容IE6以上浏览器 1. ...