自适应内容高度现在总结两种方法
方法一: 其实就是先按着自己想要的效果给弄出来,然后根据宽度得到这个控件的cgsize
NSString * stringStr =@"我是内容我是内容我是内容我是内容我是内容我是内容..";
NSMutableAttributedString * attstr = [[NSMutableAttributedString alloc] init];
//label图片文字混排
NSTextAttachment * textAttach=[[NSTextAttachment alloc]init];
textAttach.image=[UIImage imageNamed:@"abstract"];
textAttach.bounds=CGRectMake(0, 0, 42, 20);
NSAttributedString *strA = [NSAttributedString attributedStringWithAttachment:textAttach];
[attstr appendAttributedString:strA];
[attstr appendAttributedString:[[NSAttributedString alloc] initWithString:stringStr]];
//label行间距
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
[paragraphStyle setLineSpacing:10];
[attstr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [stringStr length])];
UILabel * lbzy = [[UILabel alloc]init];
lbzy.attributedText=attstr;
lbzy.lineBreakMode=NSLineBreakByWordWrapping;
lbzy.numberOfLines=0;
lbzy.font=[UIFont systemFontOfSize:13];
//调节高度
CGSize size = CGSizeMake(KWidth-20, MAXFLOAT);
CGSize labelSize = [lbzy sizeThatFits:size];
lbzy.frame = CGRectMake(10, 10,KWidth-20,labelSize.height);//设置label的frame
方法二: 这个是根据宽度得到内容的cgsize,然后赋值给label
//标题
NSString * stringStr = [@"我是标题" stringByTrimmingCharactersInSet:whitespace];
CGSize size=[stringStr sizeWithFontCompatible:[UIFont systemFontOfSize:14] forWidth:(screenW-100) lineBreakMode:NSLineBreakByWordWrapping];
_newsTitle.frame=CGRectMake(91, 0,screenW-100,size.height);
_newsTitle.lineBreakMode=NSLineBreakByWordWrapping;
_newsTitle.numberOfLines=0;
_newsTitle.text=stringStr;
NSDictionary *dictionaryAttributes = @{NSFontAttributeName:font,};
CGRect stringRect = [self boundingRectWithSize:CGSizeMake(width,MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:dictionaryAttributes
context:nil];