txtlbl.font = [UIFont boldSystemFontOfSize:14.0f];
txtlbl.numberOfLines = 0;
NSString *str = @" 阿方决定设立科技特网络离开电视剧分w额两个大陆高科技了了不见了日i倒计时离开我说老师肯德基弗兰克萨江东父老将费德勒说阿方决定设立科技特网络离开电视剧分w额两个大陆高科技了了不见了日i倒计时离开我立科说老师肯德基弗兰克萨江东父老将费德勒说";
CGSize size = [str sizeWithFont:txtlbl.font constrainedToSize:CGSizeMake(txtlbl.frame.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
//根据计算结果重新设置txtlbl的尺寸
[txtlbl setFrame:CGRectMake(7, 0, 310, size.height)];
txtlbl.text = str;
方法二:
// iOS7_API_根据文字 字数动态确定Label宽高
// 设置Label的字体 HelveticaNeue Courier
UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:24.0f];
_nameLabel.font = fnt;
// 根据字体得到NSString的尺寸
CGSize size = [_nameLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
// 名字的H
CGFloat nameH = size.height;
// 名字的W
CGFloat nameW = size.width;
_nameLabel.frame = CGRectMake(0, 0, nameW,nameH);
方法三:
// 宽度W
CGFloat contentW = self.bounds.size.width - _content.frame.origin.x - kMargin;
// label的字体 HelveticaNeue Courier
UIFont *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];
// 高度H
CGFloat contentH = tmpRect.size.height;
NSLog(@"调整后的显示宽度:%f,显示高度:%f"contentW,contentH);
_content.frame = CGRectMake(0, 0, contentW,contentH);