有时一个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. 算法5-6:Kd树

    问题 给定一系列的点.和一个矩形.求矩形中包括的点的数量. 解答 这个问题能够通过建立矩阵来进行求解.首先将一个空间切割成矩阵,将点放置在相应的格子中.再计算矩形覆盖的格子.再推断格子中的点是否包括在 ...

  2. nginx源代码分析--GDB调试

    利用gdb[i]调试nginx[ii]和利用gdb调试其他程序没有两样,只是nginx能够是daemon程序,也能够以多进程执行,因此利用gdb调试和寻常会有些许不一样. 当然,我们能够选择将ngin ...

  3. Django rest_framework 认证源码流程

    一.请求到来后,都要先执行dispatch方法 dispatch根据请求方式的不同触发get/post/put/delete等方法 注意,APIView中的dispatch方法有很多的功能 def d ...

  4. java中虚拟机命令:jstack使用方法

    Stack Trace for Java,用于生成虚拟机当前的线程快照信息,包含每一条线程的堆栈信息.该命令通常用于定位线程停顿原因,当出现线程停顿时,可通过stack查看每个线程的堆栈信息,进而分析 ...

  5. Android——保存和恢复用户状态

    onSaveInstanceState 保存 在暂停之后和保存之前调用 onRestoreInstanceState 恢复 再启动之后和显示之前调用 package com.example.chens ...

  6. 归并排序的C++实现

    原创作品,转载请注明出处:点我 归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序列 ...

  7. linux使用ip能ping通,但使用域名却不能访问的解决方法

    使用命令:yum -y update进行更新测试,一般测试结果为couldn't resolve hostmirrors.aliyun.com 解决方式参考博客couldn't resolve hos ...

  8. 扩展-Easyui Datagrid相同连续列合并扩展(一)

    一.autoMergeCellAndCells实现效果 调用方法: function onLoadSuccess(data){     $(this).datagrid("autoMerge ...

  9. 数据库 Oracle数据库对象二

    视图 --视图是对表逻辑抽象 --视图的好处:简化查询 --视图是一种虚表 --视图建立在已有表的基础上,视图赖以建立的这些吧称为基表. --向视图提供数据内容的语句为select语句,可以将视图理解 ...

  10. Yarn概述

    转自:http://liujiacai.net/blog/2014/09/07/yarn-intro/ Yarn是随着hadoop发展而催生的新框架,全称是Yet Another Resource N ...