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. ZOJ - 3862 Intersection 【贪心】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3862 思路 因为交换次数达到 n + 10 其实我们可以先将他们 ...

  2. 多线程(一) NSThread

    OS中多线程的实现方案: 技术 语言 线程生命周期 使用频率 pthread C 程序员自行管理 几乎不用 NSthread OC 程序员自行管理 偶尔使用 GCD C 自动管理 经常使用 NSOpe ...

  3. 3 《锋利的jQuery》jQuery中的DOM操作

    DOM操作分(1)DOM Core(核心):document.geElementsByTagName("form");/ element.getAttribute("sr ...

  4. 20145239杜文超 《Java程序设计》实验二 Java面向对象程序设计实验报告

    20145239 <Java程序设计>实验二 Java面向对象程序设计实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S. ...

  5. POJ 3620 Avoid The Lakes(dfs算法)

    题意:给出一个农田的图,n行m列,再给出k个被淹没的坐标( i , j ).求出其中相连的被淹没的农田的最大范围. 思路:dfs算法 代码: #include<iostream> #inc ...

  6. spark源码笔记

    1.国际化 如添加朋友Friends是英文,可以找着相关的类,并在国际化配置文件中添加key 在项目中全局搜索“Friends”,将得到的结果集全部展开,找到这两个文件: 在国际化配置文件spark_ ...

  7. haproxy透传用户ip-方法和原理

    为了透传用户ip到后端server, proxy机器需要解决两个问题: 1.在创建到后端server的套接字时, 将用户ip作为套接字的源ip,从而让后端server看到: 2.后端server在回包 ...

  8. oracle11g登录等问题

    一.oracle 11g登录服务开启 成功安装Oracle 11g后,共有7个服务,这七个服务的含义分别为:1. Oracle ORCL VSS Writer Service:Oracle卷映射拷贝写 ...

  9. JSP常见知识点

    false 7.8 磅 0 2 false false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTa ...

  10. CentOS 6以下版本 支持Ext4

    CentOS默认是不支持Ext4.所以你需要处理一下才行. 使用环境使用的是CentOS5.8 内核是  2.6.18-238.19.1.el5 其实CentOS 5.8 里面是有 ext4 模块的, ...