问题描述
- 文本多行时获取标签高度
-
在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;
如果文本很多行就无法设置标签的高度了,怎么办?
解决方案
先给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);
解决方案二:
CGFloat height=[yourlable sizeWithFont:[UIFont fontWithName:@"Arial" size:31] constrainedToSize:CGSizeMake(yourlable.frame.size.width,1000) lineBreakMode:UILineBreakModeWordWrap].height;
获取label的动态高度
时间: 2024-09-30 11:14:44