uilabel 自适应
有时一个UILable的text内容是变化的,而且差异有很大,
- 需求上要求UILabel的大小高宽能够自适应text的内容。代码例子:
- myLable=[[UILabel alloc] initWithFrame:CGRectMake(0, 23, 175, 33)];
- [myLable setFont:[UIFont fontWithName:@"Helvetica" size:10.0]];
- [myLable setNumberOfLines:0];
- [myLable setBackgroundColor:[UIColor clearColor]];
- [myAdView addSubview:myLable];
- UIFont *font = [UIFont fontWithName:@"Helvetica" size:10.0];
- CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f)
- lineBreakMode:UILineBreakModeWordWrap];
- CGRect rect=myLable.frame;
- rect.size=size;
- [myLable setFrame:rect];
- [myLable setText:text];
- 核心的是
- CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f)
- lineBreakMode:UILineBreakModeWordWrap];
- 来预算text显示时宽高。
- 其中font是显示的字体,constrainedToSize是最大可接受的字符串宽高(例子中是宽175,高2000)
- lineBreakMode换行类型(UILineBreakModeWordWrap指的单词边界换行)
sizeWithFont:constrainedToSize:lineBreakMode:
Returns the size of the string if it were rendered with the specified constraints.
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 自适应的更多相关文章
- UILabel 自适应宽高
#import <UIKit/UIKit.h> @interface UILabel (UILabel_LabelHeighAndWidth) + (CGFloat)getHeightBy ...
- UILabel自适应高、宽
根据Label和字体大小自适应高度 - (CGFloat)getHeightWithLabel:(UILabel *)label andFontSize:(CGFloat)size { label.n ...
- UILabel自适应高度,自动换行
CGRect rect; rect = self.labelInfo.frame; //UILabel高度自适应 rect.size.height = [self.labelInfo.text bou ...
- UILabel自适应高度
在网上看了一些,发现很多关于自适应高度的,不能用,就在下面写一种我常用的吧!保证可以直接粘贴复制到Xcode中运行. UILabel *label = [[UILabel alloc] init]; ...
- UILabel 自适应高度,宽度
mLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 10, 1)]; mLabel1.text = @"my label 1, ...
- iOS之UILabel自适应高度、宽度
下列两条自适应高度和宽度的自定义方法:
- UILabel自适应高度和自动换行
码: //初始化label UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)]; //设置自动行数与字符换行 [l ...
- iOS之UILabel自适应大小
//初始化一个label self.label=[[UILabel alloc] init]; //设置自动行数与字符换行 [self.label setNumberOfLines:0]; //给la ...
- UITableViewCell的高度与UILabel自适应
UITableViewCell内部只放了一个UILabel,Cell的高度随着UILabel内容的高度变化而变化,可重写UITableView的委托方法动态调整高度,还要设置UILabel.numbe ...
随机推荐
- 【Android】14.2 外部文件存储和读取
分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 1.基本概念 内部存储的私有可用存储空间一般都不会很大,对于容量比较大的文件,例如视频等,应该将其存储在外部存储设 ...
- linux - camera capture
//cut a picture#include <stdio.h>#include <stdlib.h>#include <string.h>#include &l ...
- DIV中display和visibility属性差别
DIV中display和visibility属性差别 DIV中display和visibility属性差别还是挺大的,虽然Visibility和Display属性都可以达到隐藏页面元素的目的,但它们的 ...
- Spring Boot - can't start with embedded tomcat error
com.fasterxml.jackson.core版本问题,更新最新版本即可. I had the same problem, it seems that: <dependency> & ...
- 你真的需要一个jQuery插件吗
jQuery的插件提供了一个很好的方法,节省了时间和简化了开发,避免程序员从头开始编写每个组件.但是,插件也将一个不稳定因素引入代码中.一个好的插件节省了无数的开发时间,一个质量不好的插件会导致修复错 ...
- JavaScript语言精粹读书笔记- JavaScript对象
JavaScript 对象 除了数字.字符串.布尔值.null.undefined(都不可变)这5种简单类型,其他都是对象. JavaScript中的对象是可变的键控集合(keyed collecti ...
- 第二百六十九节,Tornado框架-Session登录判断
Tornado框架-Session登录判断 Session需要结合cookie来实现 Session的理解 1.用户登录系统时,服务器端获取系统当前时间,进行nd5加密,得到加密后的密串 2.将密串作 ...
- iScroll框架解析——Android 设备页面内 div(容器,非页面)overflow:scroll; 失效解决(转)
移动平台的活,兼容问题超多,今儿又遇到一个.客户要求在弹出层容器内显示内容,但内容条数过多,容器显示滚动条.按说是So easy,容器设死宽.高,CSS加属性 overflow:scroll; -we ...
- php -- php数组相关函数
array range ( mixed $low , mixed $high [, number $step ] ) 创建一个连续的数组 range('a','z'); foreach (range( ...
- linux下解压 tar.bz2
tar xvfj xxx.tar.bz2 转自: http://www.360doc.com/content/12/0907/16/8006573_234845810.shtml