iOS UILable 高度自适

#import "ViewController.h"
#define FontSize 20

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *str = @"相见欢李煜  
春花秋月何时了,
往事知多少。
小楼昨夜又东风,
古国不堪回首月明中。
雕栏玉砌应有在,
只是朱颜改。
问君能有几多愁,
恰似一江春水向东流。"

    CGRect viewRect ;
    viewRect.origin.x = 0;
    viewRect.origin.y = 0;
    viewRect.size.width = self.view.frame.size.width;
    viewRect.size.height = [str sizeWithFont:[UIFont systemFontOfSize:FontSize] constrainedToSize:CGSizeMake(viewRect.size.width,9999)].height;
    //viewRect.size = [str sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(viewRect.size.width,99999) lineBreakMode:NSLineBreakByWordWrapping] ;
    UILabel *label = [[UILabel alloc]initWithFrame:viewRect];
    //label.lineBreakMode = NSLineBreakByWordWrapping;
    [label setText:str];
    [label setFont:[UIFont systemFontOfSize:FontSize]];
    [label setNumberOfLines:0];
    [self.view addSubview:label];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
原文地址:https://www.cnblogs.com/lihaibo-Leao/p/3422602.html