ios--动态计算label的高度

#import "ViewController.h"

@interface ViewController ()

{

    UILabel *dynamicLabel;

 

}

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

   

    if (!dynamicLabel) {

        dynamicLabel=[[UILabel alloc]init];

    }

        dynamicLabel.backgroundColor=[UIColor redColor];

        dynamicLabel.font=[UIFont fontWithName:@"HelveticaNeue" size:24.0f];

        dynamicLabel.text=@"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus eu urna quis lacus imperdiet scelerisque a nec neque. Mauris eget feugiat augue, vitae porttitor mi. Curabitur vitae sollicitudin augue. Donec id sapien eros. Proin consequat tellus in vehicula sagittis. Morbi sed felis a nibh hendrerit hendrerit. Lorem ipsum dolor sit.";

        dynamicLabel.lineBreakMode=NSLineBreakByWordWrapping;

        dynamicLabel.numberOfLines = 0;

        CGRect tempRect=[dynamicLabel.text boundingRectWithSize:CGSizeMake(300,1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:dynamicLabel.font,NSFontAttributeName, nil] context:nil];

        dynamicLabel.frame=CGRectMake(10, 64, tempRect.size.width,  tempRect.size.height);

    

        [self.view addSubview:dynamicLabel];

}

posted @ 2014-11-25 14:29  曹县三胖暴打大猩猩  阅读(169)  评论(0编辑  收藏  举报