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 ...
随机推荐
- CWidgetMgr---cpp
#include "WidgetMgr.h" #include "XWidget.h" #include "Config.h" #inclu ...
- UIWebView和UICollectionViewController的使用
UIWebView和UICollectionViewController的使用 UIWebView UIWebView是iOS内置的浏览器的控件, 可以浏览网页, 打开文档等 .系统自带的Safari ...
- JS四种方法去除字符串最后的逗号
<script> window.onload=function() { var obj = {name: "xxx", age: 30, sex: "fema ...
- 字符集导致乱码问题,gi安装问题
今天是2014-4-24,今天中午收到一个天津网友问的一个安装gi的问题,和一个网友问的字符集问题:在此整理一下 问题一: gi安装问题: 问题描写叙述: 在安装gi的时候提示:"INS-2 ...
- 如何对MySQL中的大表进行数据归档
使用MySQL的过程,经常会遇到一个问题,比如说某张”log”表,用于保存某种记录,随着时间的不断的累积数据,但是只有最新的一段时间的数据是有用的:这个时候会遇到性能和容量的瓶颈,需要将表中的历史数据 ...
- C51寄存器详解(Reg51.h)
Reg51.h 这个头文件将C程序中能用到的寄存器名或寄存器中某位的名称与硬件地址值做了对应,在程序中直接写出这些名称,集成开发环境就能识别,并最终转换成机器代码,实现对单片机各硬件资源的准确操控. ...
- C++ 引用做左值
//引用做左值 #include<iostream> using namespace std; int SetA(int *p){ *p = ; return *p; } int& ...
- C# 中的treeview绑定数据库(递归算法)
近日面试的给我两道题目,一道是IQ测试,第二个就是题目所言 总共两个表 department(id int not null primary key,parentid int,name char(50 ...
- RabbitMQ OS X下安装及常用命令-1
RabbitMQ的主页在http://www.rabbitmq.com/ . 1. 安装Erlang RabbitMQ是用Erlang编写的,所以需要先安装Erlang,如果有的话跳过 ...
- UIWindow小记
If you choose to create a window in Interface Builder, be sure to select the Full Screen at Launch o ...