首页 > 代码库 > UILabel根据内容自动调整高度

UILabel根据内容自动调整高度

效果图:

代码:

- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.        //根据内容自动调整高度    NSString *str = @"公元前3000年,印度河流域的居民的数字使用就已经比较普遍,居民们采用了十进位制的计算法。";    UIFont *font = [UIFont systemFontOfSize:13];    CGSize size = CGSizeMake(200,2000);    CGRect labelRect = [str boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)  attributes:[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName] context:nil];        //UILabel    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50,100, labelRect.size.width, labelRect.size.height)];    label.text = str;    label.backgroundColor=[UIColor redColor];    label.font = [UIFont systemFontOfSize:13];    label.numberOfLines=0;    [self.view addSubview: label];}

 

UILabel根据内容自动调整高度