有时一个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. 【转】Appium_API(翻译+用法说明)

    转自:https://testerhome.com/topics/3711 1.contextscontexts(self): Returns the contexts within the curr ...

  2. UVA - 1218 Perfect Service(树形dp)

    题目链接:id=36043">UVA - 1218 Perfect Service 题意 有n台电脑.互相以无根树的方式连接,现要将当中一部分电脑作为server,且要求每台电脑必须连 ...

  3. StringJDBC更改数据库的两种方式

    方法一jdbcTemplate.update("INSERT INTO USER VALUES(?, ?, ?, ?)", new Object[] {user.getId(), ...

  4. 理解vertical-align或“如何竖向居中”<转>

    在各种技术论坛里经常会有这样的问题提出,“我如何能将这个东西竖向居中?”这个问题通常会跟随着这样一句话,“我使用了 vertical-align:middle,但是不管用! ” 这个问题其实有三个层面 ...

  5. C#中利用JQuery实现视频网站的缩略图采集

    最近有朋友想要采集优酷的视频标题和缩略图 (哈哈, 并非商业目的). 找到我帮忙, 考虑到有我刚刚发布的SpiderStudio, 我毫不犹豫的答应了. 首先在网页上视频的基本结构为: div.v - ...

  6. LVM简介

    3. 创建VG.. 7 4. 创建LV.. 9 5.LV格式化及挂载... 10 一.LVM简介 LVM是 Logical Volume Manager(逻辑卷管理)的简写,它由Heinz Mauel ...

  7. EF常用查询语句

    //方法一 Linq to Entities            var info = from p in entity.Users where p.ID >= 10 orderby p.ID ...

  8. VSS (Visual Source Safe 2005) 用法详解

    VSS用法指南 The usage of VSS (Visual Source Safe 2005) 1. 首先,当然是得安装好Visual Source Safe 2005 你可以在Visual S ...

  9. ThinkPHP项目笔记之RBAC(权限)中篇

    现在,说说添加权限,权限管理列表 c.添加权限

  10. w.5 Y k J.COm 9 如何做好总经理助理

    总经理助理的工作职责:(指要做的事—做事的责任) 忠诚的总管家----总经理的代言人和执行官. 总经理助理一方面要为企业发展运筹帷幄,另一方面更要分担繁重的行政事务,所以他务必履行对该职业的使命感. ...