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 ...
随机推荐
- [ukulele]入门指南
不少尤克里里初学者在开始学习尤克里里的时候,都会进行尤克里里教程搜索,还有不少新手会问“尤克里里和吉他的区别”“尤克里里好学吗”“尤克里里和弦有哪些”等问题.今天,国际知名乐器品牌Gorilla歌芮拉 ...
- UVA - 1218 Perfect Service(树形dp)
题目链接:id=36043">UVA - 1218 Perfect Service 题意 有n台电脑.互相以无根树的方式连接,现要将当中一部分电脑作为server,且要求每台电脑必须连 ...
- PHP 生成唯一的激活码
<? php /** * 生成永远唯一的激活码 * @return string */ function create_guid($namespace = null) { static $gui ...
- 每日英语:How to find the career of your dreams
The fate described by Dostoyevsky is a nightmare we all hope to escape. But we're surrounded by nays ...
- [C++]在什么时候需要“#include string.h“
相关资料:https://zhidao.baidu.com/question/515578726.html C++中,string头文件基本上已经包含在iostream中了.但是,平时使用的时候建议加 ...
- PHP——小尾巴之权限管理
流程: 在权限管理页面,默认显示用户的角色,更改复选框的按钮内容,可以改变角色,点击确定提交至数据库 在登陆之后,只显示该用户的角色所对应的权限 数据库: guanli.php <!DOCTYP ...
- 亿级日PV的魅族云同步的核心协议与架构实践(转)
云同步的业务场景 这是魅族云同步的演进,第一张是M8.M9,然后到后面的是MX系统,M9再往后发展,我们的界面可以看到基本上是没有什么变化的,但本质发生了很大的变化,我们经过了一些协议优化,发展到今天 ...
- 燕十八mysql笔记
mysql复习 一:复习前的准备 1:确认你已安装wamp 2:确认你已安装ecshop,并且ecshop的数据库名为shop 二 基础知识: 1.数据库的连接 mysql -u -p -h -u 用 ...
- C语言 百炼成钢22
/* 题目58: 编写一个业务函数,实现按行读取文件.把内容按照第三种内存模型打包数据传出,把行数通过函数参数传出. 函数原型有两个,任意选择其一 要求1:请自己任意选择一个接口(函数),并实现功能: ...
- [web开发] php优势 - PHP与ASP.NET的比较
php 优势 - PHP与ASP.NET的比较 如今当提到 Web 开发时,您有许多选择.这些方法中许多都涉及到预处理 — 即,利用特定的标记将代码嵌入到 HTML 页面中,这些标记告诉预处理器,它们 ...