现在的位置: 首页 > 综合 > 正文

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

2013年07月12日 ⁄ 综合 ⁄ 共 459字 ⁄ 字号 评论关闭

 

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赋值让其自动调整其大小了。其实宽度可以设置为固定,让其改高度更好。

抱歉!评论已关闭.