有时一个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. C++之把流对象当做函数参数传递

    一.编译不通过的代码: /******************************************************************************* * File ...

  2. CSS学习笔记(6)--浮动,三列布局,高度宽度自适应

    百度ife任务三,要求中间宽度自适应,高度取三列最高者. 开始用position的relative和absolute,但是relative不能自适应宽,absolute不能加float浮动,撑不起来外 ...

  3. linux回调函数的使用

    #include<stdio.h> #include<pthread.h> #include<unistd.h> pthread_mutex_t mutex; pt ...

  4. beaglebone black ubuntu display x11 server的配置

     Change default resolution on BeagleBone modesetting vs fbdev digiteltlc May 7th, 2014, 03:28 PM Hi ...

  5. per-cpu

    What is percpu data? percpu data 是内核为smp系统中不同CPU之间的数据保护方式,系统为每个CPU维护一段私有的空间,在这段空间中的数据只有这个CPU能访问.但是这种 ...

  6. Maven + Spring 进行多环境自动切换功能

    在pom.xml的<project></project>的最下放写入如下代码: <!-- profiles setting start [mvn install -P x ...

  7. Golang HTTP文件上传

    2018年02月08日 10:07:13 冷月醉雪 阅读数:346 标签: golangHTTP文件上传更多 个人分类: Go   版权声明:本文为博主原创文章,未经博主允许不得转载. https:/ ...

  8. 数据库 proc编程六

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...

  9. JS侦测设备旋转方向

    window.onload = window.onresize = function initialLoad(){updateOrientation();} function updateOrient ...

  10. WebService的初级学习

    复习准备 1. Schema约束: 1.1   namespace相当于Schema文件的id: 1.2   targetNamespace属性用来指定schema文件的namespace的值; 1. ...