IOS7中自动计算label的宽度和高度的方法
#import "ViewController.h" @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; //根据固定的宽度计算 计算label的高度
[self sizeToLabelHeight]; //根据固定的高度 计算label的宽度
[self sizeToLabelWidth]; } /**
* 自动计算label的宽度 前提高度固定
*
*/
- (void)sizeToLabelWidth
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:];
label.numberOfLines = ; //这个属性 一定要设置为0 0表示自动换行 默认是1 不换行
label.backgroundColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentLeft;
NSString *str = @"fsdfsfnksdfjsdkhfjksdhfjdolfsdfsfnksdfjsdkhfjksdhfjsdkhfjksdhfjdojdol"; //第一种方式
// CGSize size = [str sizeWithFont:label.font constrainedToSize: CGSizeMake(MAXFLOAT,label.frame.size.height) lineBreakMode:NSLineBreakByWordWrapping]; //第二种方式 NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:]; CGSize size = [str boundingRectWithSize:CGSizeMake( MAXFLOAT,label.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size; label.frame = CGRectMake(, , size.width, );
label.text = str; [self.view addSubview:label];
} /**
* 自动计算label的高度 前提 :宽度固定
*/
- (void)sizeToLabelHeight
{ UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:];
label.numberOfLines = ;//这个属性 一定要设置为0 0表示自动换行 默认是1 不换行
label.backgroundColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentLeft; NSString *str = @"fsdfsfnksdfjsdkhfjksdhfjdolfsdfsfnksdfjsdkhfjksdhfjsdkhfjksdhfjdojdol"; //第一种方式
// CGSize size = [str sizeWithFont:label.font constrainedToSize: CGSizeMake(label.frame.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping]; //第二种方式
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:]; CGSize size = [str boundingRectWithSize:CGSizeMake(label.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size; label.frame = CGRectMake(, , , size.height);
label.text = str; [self.view addSubview:label];
} @end
IOS7中自动计算label的宽度和高度的方法的更多相关文章
- HTML 的超链接 a 标签中如何设置其宽度和高度?
HTML 的超链接 a 标签中如何设置其宽度和高度? 在DIV CSS布局中,html 中 a 超链接标签,直接对其设置宽度和高度不能生效,设置宽度和高度也不起作用,这里为大家分享如何实现 a 标签宽 ...
- swift计算label动态宽度和高度
swift计算label动态宽度和高度 func getLabHeigh(labelStr:String,font:UIFont,width:CGFloat) -> CGFloat { let ...
- Javascript获取图片原始宽度和高度的方法详解
前言 网上关于利用Javascript获取图片原始宽度和高度的方法有很多,本文将再次给大家谈谈这个问题,或许会对一些人能有所帮助. 方法详解 页面中的img元素,想要获取它的原始尺寸,以宽度为例,可能 ...
- 如何在onCreate方法中获取视图的宽度和高度
你可以通过视图的getWidth()和getHeight()来获取视图的宽度和高度. 但是,可能会让你失望的是,如果你直接在onCreate方法内调用这两个函数,你会的到0. 为什么呢? 这是因为,当 ...
- Js中获取显示器、浏览器以及窗口等的宽度与高度的方法
网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWid ...
- HackThirteen 在onCreate()方法中获取View的宽度和高度
1.概要: Android源代码中很多模块都使用了post()方法,深入理解框架曾运行机制对于避开类似于本例中的小陷阱是很重要的 2.问题提出: 如果开发一些依赖于UI控件的宽和高的功 ...
- 在js中获取上传图片的宽度和高度
Html: <input type="file" id="MapUploadTd" onchange="getMapPictureSize(th ...
- js各种获取当前窗口页面宽度、高度的方法
alert($(window).height()); //浏览器时下窗口可视区域高度 alert($(document).height()); //浏览器时下窗口文档的高度 alert($(docum ...
- WPF: 共享Grid宽度或高度的方法
需要两个属性: 1. Grid.IsSharedSizeScope="True" 2. SharedSizeGroup=名称 <StackPanel Margin=" ...
随机推荐
- 那些在学习iOS开发前就应该知道的事(part 2)
英文原文:Things I wish I had known before starting iOS development—Part 2 http://www.cocoachina.com/ios/ ...
- [AY技术分享]WPF AYUI的高大上日历代码
看到这里,也谢谢大家关注了AYUI 这次讲的是AY最近没事开发的AyDatePicker,先看效果图 SelectMode=DateTime模式 SelectMode=OnlySelectDate模式 ...
- 定时5秒之后驻留在元素ID为content元素的内容
如果我只能刷新一个特定的页面的一部分,这将是很大的,例如:仪表盘上的交通灯显示系统状态. 这是很容易通过使用jQuery JavaScript库,只刷新页面的一部分.一旦我们纳入我们的页面的jQuer ...
- LeetCode: Path Sum II 解题报告
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- Python中的模块与包
标准库的安装路径 在import模块的时候,python是通过系统路径找到这些模块的,我们可以将这些路径打印出来: >>> pprint.pprint(sys.path) ['', ...
- windbg常用命令
SRV*C:\Symbols*http://msdl.microsoft.com/download/symbols CPU常用命令 载入sos.dll 执行.load C:\Windows\Micr ...
- 一些值得学习和借鉴的.Net 开源项目
1.DotNetFramework .NET Reference Source 发布了 beta 版,可以在线浏览 .NET Framework 4.5.1 的源代码,并且可以通过配置,在 Visua ...
- Selenium自动化测试实践 公开班(广州)
Selenium自动化测试实践 公开班(广州) http://gdtesting.com/product.php?id=115
- 如何重置CentOS 7的Root密码?设置CentOS 7的Root密码的方法与步骤
- Reactor模式与Proactor模式
该文章总结了网上资源对这两种模式的描述 原文地址:http://www.cnblogs.com/dawen/archive/2011/05/18/2050358.html 1.标准定义 两种I/O多路 ...