IOS 6和ios7 不一样,所以,我们分开来:
IOS6:
//计算实际frame大小,并将label的frame变成实际大小 CGSize size01 = [ssizeWithFont:[UIFontsystemFontOfSize:15.0f]constrainedToSize:sizelineBreakMode:NSLineBreakByWordWrapping]; [label setFrame:CGRectMake(10,50, size01.width, size01.height)];
IOS7:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 46)]; NSString *str=@"发恶无法奶奶问服务就啃发觉我看能否尽快稳罚款金额为你放假"; label.text=str; UIFont *font=[UIFont systemFontOfSize:15]; label.font=font; //拆行 label.lineBreakMode=NSLineBreakByWordWrapping; //设置多行 [label setNumberOfLines:0]; //设置最大尺寸的宽和高 CGSize size=CGSizeMake(100, 1000); //获取当前字体 NSDictionary *dict=@{NSFontAttributeName: [UIFont systemFontOfSize:15]}; CGSize authSize=[str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size; label.frame=CGRectMake(20, 70, authSize.width, authSize.height); [self.view addSubview:label];