根据文字 字数动态确定Label宽高
//设置Label的字体 HelveticaNeue CourierUIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:24.0f];_nameLabel.font = fnt;//根据字体得到NSString的尺寸CGSize size = [_nameLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];//名字的高度CGFloat nameH = size.height;//名字的宽度CGFloat nameW = size.width; //重新给Label的frame赋值_nameLabel.frame = CGRectMake(0, 0, nameW,nameH);
根据正文内容多少,动态确定正文content的frame
// 宽度W CGFloat contentW = self.bounds.size.width - _content.frame.origin.x - kMargin;// label的字体 HelveticaNeue CourierUIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:18.0f];_content.font = fnt;_content.numberOfLines = 0;_content.lineBreakMode = NSLineBreakByWordWrapping;// iOS7中用以下方法替代过时的iOS6中的sizeWithFont:constrainedToSize:lineBreakMode:方法CGRect tmpRect = [_content.text boundingRectWithSize:CGSizeMake(contentW, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil] context:nil];// 高度HCGFloat contentH = tmpRect.size.height;NSLog(@"调整后的显示宽度:%f,显示高度:%f"contentW,contentH);_content.frame = CGRectMake(0, 0, contentW,contentH);