调整UILabel的大小并不完全正常
我正在调整表格的cell
和UILabel
。 标签似乎被resize,但仍然在一行。 如何处理这个?
标签从故事板中设置为:
Line Breaks: Word Wrap; Autoshrink: Fixed Font Size; Lines: 0
CGRect textRect = [cell.sampleLabel.text boundingRectWithSize:boundingSize options:NSStringDrawingUsesLineFragmentOrigin attributes: @{NSFontAttributeName:cell.sampleLabel.font} context:nil]; NSLog(@"textRect height: %f", textRect.size.height); cell.sampleLabel.frame = textRect; NSLog(@"label height: %f", cell.sampleLabel.frame.size.height);
NSLog
:
2013-12-20 11:04:41.623 DevCloud[16613:70b] textRect height: 223.091019 2013-12-20 11:04:41.624 DevCloud[16613:70b] label height: 224.000000 2013-12-20 11:04:41.626 DevCloud[16613:70b] textRect height: 223.091019 2013-12-20 11:04:41.627 DevCloud[16613:70b] label height: 224.000000
试试这个
cell.lbl_view2_disc1.numberOfLines=1000; CGSize maximumLabelSize = CGSizeMake(260, FLT_MAX); CGSize expectedLabelSize = [cell.lbl_view2_disc1.text sizeWithFont:calibri constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByCharWrapping];
如果您的UIlabel宽度修复,然后设置maximumLabelSize中的修复宽度
CGSize maximumLabelSize = CGSizeMake(260, FLT_MAX);
如果你的UIlabel高度修正,然后在maximumLabelSize中设置UIlabel.frame.size.height的修复高度
CGSize maximumLabelSize = CGSizeMake(FLT_MAX,cell.lbl_view2_disc1.frame.size.height);
在我的代码工作
#define FONT_SIZE 14.0f #define CELL_CONTENT_WIDTH 280.0f #define CELL_CONTENT_MARGIN 40.0f -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *text = @"your string"; CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN ), 2000.0f); CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping]; CGFloat height = MAX(size.height, 20.0f); cellheight=height + (CELL_CONTENT_MARGIN * 2); return height + (CELL_CONTENT_MARGIN * 2); } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UILabel *label = nil; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } for (UIView *view in cell.contentView.subviews) { [view removeFromSuperview]; } NSString *text = @"your string"; CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN ), 2000.0f); CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping]; commentscontentView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 290, MAX(size.height, 44.0f))]; commentscontentView.tag=111; [cell.contentView addSubview:commentscontentView]; label = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 220, 20)]; [label setLineBreakMode:NSLineBreakByWordWrapping]; [label setMinimumFontSize:FONT_SIZE]; label.backgroundColor=[UIColor clearColor]; [label setNumberOfLines:0]; [label setFont:[UIFont fontWithName:@"OpenSans-Light" size:FONT_SIZE]]; [label setTag:1]; [commentscontentView addSubview:label]; if (!label) label = (UILabel*)[cell viewWithTag:1]; [label setText:text]; [label setFrame:CGRectMake(20, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN), MAX(size.height, 44.0f))]; label.textColor=[UIColor blackColor]; [label setBackgroundColor:[UIColor clearColor]]; return cell; }
尝试
cell.sampleLabel.numberOfLines = 0
尝试
cell.sampleLabel.numberOfLines = 0 [cell.sampleLabel sizeToFit];