这样的话,会根据Label上展示数据的多少,自己控制高度,完全使Label适应文字;这样可以获取Label的宽和高,不至于扰乱Label以下的布局。
UILabel *titleLabel = [UILabel new];
titleLabel.font = [UIFont systemFontOfSize:18];
NSString *titleContent = @"上边设置Label字体是多大,下边titleSize中的字体也要设置成多大,才能使Label的大小完全是文字的大小";
titleLabel.text = titleContent;
titleLabel.numberOfLines = 0;
titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
CGSize titleSize = [titleContent boundingRectWithSize: CGSizeMake(SCREEN_WIDTH-10, MAXFLOAT) options: NSStringDrawingUsesLineFragmentOrigin attributes: @{NSFontAttributeName: [UIFont systemFontOfSize: 18] } context: nil].size;
titleLabel.frame = CGRectMake(10,150, titleSize.width, titleSize.height);
NSLog(@"高度为:%f",titleSize.height);
NSLog(@"宽度为:%f",titleSize.width);
titleLabel.backgroundColor = [UIColor greenColor];
[self.view addSubview: titleLabel];