让label自适应里面的文字,自动调整宽度和高度的

来源:互联网 发布:淘宝侵犯商标权 编辑:程序博客网 时间:2024/05/18 00:59

http://linglong117.blog.163.com/blog/static/27714547201076103317772/

 

让label自适应里面的文字,自动调整宽度和高度的

iphone 2010-08-06 10:33:17 阅读108 评论0  字号: 订阅

http://www.cocoachina.com/iphonedev/sdk/2010/0802/1958.html

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赋值让其自动调整其大小了。

原创粉丝点击