有时一个UILable的text内容是变化的,而且差异有很大,

  1. 需求上要求UILabel的大小高宽能够自适应text的内容。代码例子:
  2. myLable=[[UILabel alloc] initWithFrame:CGRectMake(0, 23, 175, 33)];
  3. [myLable setFont:[UIFont fontWithName:@"Helvetica" size:10.0]];
  4. [myLable setNumberOfLines:0];
  5. [myLable setBackgroundColor:[UIColor clearColor]];
  6. [myAdView addSubview:myLable];
  7. UIFont *font = [UIFont fontWithName:@"Helvetica" size:10.0];
  8. CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f)
  9. lineBreakMode:UILineBreakModeWordWrap];
  10. CGRect rect=myLable.frame;
  11. rect.size=size;
  12. [myLable setFrame:rect];
  13. [myLable setText:text];
  14. 核心的是
  15. CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f)
  16. lineBreakMode:UILineBreakModeWordWrap];
  17. 来预算text显示时宽高。
  18. 其中font是显示的字体,constrainedToSize是最大可接受的字符串宽高(例子中是宽175,高2000)
  19. lineBreakMode换行类型(UILineBreakModeWordWrap指的单词边界换行)

sizeWithFont:constrainedToSize:lineBreakMode:

Returns the size of the string if it were rendered with the specified constraints.

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode
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 自适应的更多相关文章

  1. UILabel 自适应宽高

    #import <UIKit/UIKit.h> @interface UILabel (UILabel_LabelHeighAndWidth) + (CGFloat)getHeightBy ...

  2. UILabel自适应高、宽

    根据Label和字体大小自适应高度 - (CGFloat)getHeightWithLabel:(UILabel *)label andFontSize:(CGFloat)size { label.n ...

  3. UILabel自适应高度,自动换行

    CGRect rect; rect = self.labelInfo.frame; //UILabel高度自适应 rect.size.height = [self.labelInfo.text bou ...

  4. UILabel自适应高度

    在网上看了一些,发现很多关于自适应高度的,不能用,就在下面写一种我常用的吧!保证可以直接粘贴复制到Xcode中运行. UILabel *label = [[UILabel alloc] init]; ...

  5. UILabel 自适应高度,宽度

    mLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 10, 1)]; mLabel1.text = @"my label 1, ...

  6. iOS之UILabel自适应高度、宽度

    下列两条自适应高度和宽度的自定义方法:

  7. UILabel自适应高度和自动换行

    码: //初始化label UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)]; //设置自动行数与字符换行 [l ...

  8. iOS之UILabel自适应大小

    //初始化一个label self.label=[[UILabel alloc] init]; //设置自动行数与字符换行 [self.label setNumberOfLines:0]; //给la ...

  9. UITableViewCell的高度与UILabel自适应

    UITableViewCell内部只放了一个UILabel,Cell的高度随着UILabel内容的高度变化而变化,可重写UITableView的委托方法动态调整高度,还要设置UILabel.numbe ...

随机推荐

  1. 华为OJ训练之 简易的银行排号叫号系统

    闯关第五关的题目,一个中级题和一个高级题.中间题比較简单,半个小时完毕了.题目例如以下 实现一个简易的银行排号叫号系统 get    取号                     演示样例:" ...

  2. 修改JQM的默认配置属性

    从本文开始,使用 jQuery Mobile 与 HTML5 开发 Web App 系列将会进入第三部分——jQuery Mobile 事件与方法,这其中将会利用之前所讲述的 jQuery Mobil ...

  3. Apple ID地区怎么改为美国?(转载)

    Apple ID地区怎么改为美国?有时候我们想要去App Store中下载国外APP的话,就可以尝试将Apple ID地区改为美国,然后再打开App Store,就可以切换到美国应用市场,下载国外AP ...

  4. dp之01背包hdu2639(第k优解)

    http://acm.hdu.edu.cn/showproblem.php?pid=2639 题意:给出一行价值,一行体积,让你在v体积的范围内找出第k大的值.......(注意,不要 和它的第一题混 ...

  5. am335x 打开内部 RTC

    AM335X 打开内部 RTC 过程记录. kernel version: 3.2.0 先在 make menuconfig 里面打开内部RTC的配置: make menuconfig Device ...

  6. js、jquery遍历对象

    js遍历对象: var obj = {a:1,b:2,c:3} for(var s in obj){ alert(obj[s]); } jquery遍历对象: var obj = {a:1,b:2,c ...

  7. centos7系统根目录扩容

    比如 点击了后 点击创建虚拟磁盘 选择一个 20G  然后启动虚拟机使用fdisk查看所有的磁盘 看是否新增了一个20G的硬盘 [root@localhost ~]# fdisk -l 磁盘 /dev ...

  8. CentOS 7使用systemctl如何补全服务名称

    CentOS 7使用systemctl如何补全服务名称 因为CentOS7的默认安装类型是最小安装,所以默认没有自动补全的功能.要启用这个功能,你需要安装一个bash-completion包,然后退出 ...

  9. SSIS 自测题-文件操作类

    说明:以下是自己的理解答案,不是标准的答案,如有不妥烦请指出.         有些题目暂时没有答案,有知道的请留言,互相学习,一起进步. 1.什么是控制流,什么是数据流,控制流和数据流之间的关系是什 ...

  10. Oracle触发器修改数据时同步执行插入该条数据

    原创:lixx ---从UNWIREDATA 表中查询数据,如果该表中地磁状态发生改变(CARDSTATE)执行插入到MAINTABLE 表中---根据唯一值ERID判断,如果ERID值存在,修改该条 ...