UILabel的自适应高度iOS7.0以上版本
2015-03-07 10:01
344 查看
看到别的博客写的自适应方法用了下却不能用:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];这个frame是初设的,没关系,后面还会重新设置其size。
[label setNumberOfLines:0];
NSString *s = @"string......";
UIFont *font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(320,2000);
CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake:(0,0, labelsize.width, labelsize.height)];
[self.view addSubView:label];
这样就可以对s赋值让其自动调整其大小了。
进入API看了下才发现问题:NS_DEPRECATED_IOS(2_0, 7_0, "Use -boundingRectWithSize:options:attributes:context:");
这个方法只能在ios2.0--iOS7.0版本之间使用,我的版本较高,需要使用:-boundingRectWithSize:options:attributes:context:方法:
UILabel的自适应高度的方法:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,200,100,50)];
label.numberOfLines = 0;
NSDictionary *strAtt = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
NSString *value = @"hxdksa noasd njkla n";
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGRect rectStr = [value boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:strAtt context:nil];
label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width, rectStr.size.height);
[label sizeToFit];
[self.view addSubview:label];
[label release];
问题解决!!!
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];这个frame是初设的,没关系,后面还会重新设置其size。
[label setNumberOfLines:0];
NSString *s = @"string......";
UIFont *font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(320,2000);
CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake:(0,0, labelsize.width, labelsize.height)];
[self.view addSubView:label];
这样就可以对s赋值让其自动调整其大小了。
进入API看了下才发现问题:NS_DEPRECATED_IOS(2_0, 7_0, "Use -boundingRectWithSize:options:attributes:context:");
这个方法只能在ios2.0--iOS7.0版本之间使用,我的版本较高,需要使用:-boundingRectWithSize:options:attributes:context:方法:
UILabel的自适应高度的方法:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,200,100,50)];
label.numberOfLines = 0;
NSDictionary *strAtt = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
NSString *value = @"hxdksa noasd njkla n";
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGRect rectStr = [value boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:strAtt context:nil];
label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width, rectStr.size.height);
[label sizeToFit];
[self.view addSubview:label];
[label release];
问题解决!!!
相关文章推荐
- iOS7.0以上(含7.0)xib中UILabel自适应高度
- IOS开发—IOS7.0以后UILabel高度自适应设置
- IOS 7.0以后的UILabel自适应高度解决办法.
- IOS 7.0以后的UILabel自适应高度解决办法.
- IOS7.0 UILabel实现自适应高度的新方法
- Ios UILabel 自适应 文本高度
- IOS TableView的Cell高度自适应,UILabel自动换行适应
- IOS TableView的Cell高度自适应,UILabel自动换行适应
- IOS TableView的Cell高度自适应,UILabel自动换行适应
- iOS开发——UILabel自动换行及高度自适应UILabel自动换行及高度自适应
- ios TableView的Cell高度自适应,UILabel自动换行适应
- IOS中的UILabel中的高度自适应
- ios 文字 控件 自适应 高度 宽度 CustomCell 自适应高度+uilabel自动换行+ UITextView 根据内容自动调整高度
- iOS-UILabel的宽度,高度自适应文字
- IOS之UILabel自适应高度
- IOS UILabel自适应里面的文字,自动调整宽度和高度的
- 【版本适配】iOS中文本的自适应高度
- iOS开发- UILabel 自动换行 及 高度自适应
- iOS UILabel自适应里面的文字,自动调整宽度和高度
- iOS开发笔记--UILabel和UITextView自适应文本高度代码