2 o 1001 o O_1001_O 于 2013.08.09 11:34 提问

文本多行时获取标签高度

在detail视图中创建标签。设置相应文本时需要设置标签的高度。

NSString *mytitleString = [discussionData valueForKey:@"Name"];
 CGSize size =  [mytitleString sizeWithFont:[UIFont systemFontOfSize:14.0]  constrainedToSize:CGSizeMake(500, CGFLOAT_MAX)];
 float lbltitleheight = size.height;
 lbltitle.text = mytitleString;

如果文本很多行就无法设置标签的高度了,怎么办?

2个回答

zhanglei5415
zhanglei5415   Rxr 2013.08.09 11:42
已采纳

先给UILabel赋文本,再重新计算,重置uilabel的frame

NSString *mytitleString = [discussionData valueForKey:@"Name"];
 lbltitle.text = mytitleString;
 lbltitle.font=[UIFont systemFontOfSize:14.0];

 CGSize size = [lbltitle.text sizeWithFont:lbltitle.font  constrainedToSize:CGSizeMake(500, CGFLOAT_MAX)];
/////重置高度
lbltitle.frame=CGRectMake(lbltitle.frame.origin.x,lbltitle.frame.origin.y,size.width,size.height);
DaffodilGirl
DaffodilGirl   2013.08.09 16:50
CGFloat height=[yourlable sizeWithFont:[UIFont fontWithName:@"Arial" size:31] constrainedToSize:CGSizeMake(yourlable.frame.size.width,1000) lineBreakMode:UILineBreakModeWordWrap].height; 

获取label的动态高度

Csdn user default icon
上传中...
上传图片
插入图片