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 = @"相见欢李煜  \n春花秋月何时了,\n往事知多少。\n小楼昨夜又东风,\n古国不堪回首月明中。\n雕栏玉砌应有在,\n只是朱颜改。\n问君能有几多愁,\n恰似一江春水向东流。"

    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

 

posted on 2013-11-14 09:01  Hai_阔天空  阅读(264)  评论(0)    收藏  举报

导航