ios - 调整 UILabel 的大小以适应自定义 UITableViewCell 中的文本,无论宽度如何

标签 ios uitableview uilabel

无论设备或方向如何,我都试图让单元格中的标签大小合适。我能够正确调整行高的大小。我还能够在 cellForRowAtIndexPath 中正确设置标签高度,并且可以在我的日志中进行检查。但是,当它到达 willDisplayRowAtIndexPath 时,标签高度已经改变,但前提是单元格不是 320 磅宽。

这是我的代码-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCellIdentifier";
CustomInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
    cell = [[CustomInfoCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"CustomInfoCell" owner:self options:nil];
    cell = objects[0];
}

// Configure the cell...
cell.customTitleLabel.text = [_data[indexPath.row] objectForKey:t];

CGFloat labelWidth = self.view.frame.size.width-40;
NSLog(@"labelWidth:%f",labelWidth);

NSString *text = [_data[indexPath.row] objectForKey:d];//correct text
CGSize labelsize=[text sizeWithFont:cell.customDetailLabel.font constrainedToSize:CGSizeMake(labelWidth, 2000.0) lineBreakMode:cell.customDetailLabel.lineBreakMode];
NSLog(@"labelsize:%f,%f",labelsize.width,labelsize.height);

//For testing
cell.customDetailLabel.backgroundColor = [UIColor redColor];
NSLog(@"Pre: %@",cell.customDetailLabel);
cell.customDetailLabel.frame=CGRectMake(20, 22, labelWidth, labelsize.height);

cell.customDetailLabel.text = text;
    NSLog(@"Post: %@",cell.customDetailLabel);
return cell;
}

willDisplayRowAtIndexPath 中,我还打印了标签信息。这是一行打印的内容-

2013-03-24 18:33:44.009 Bridge Alert[57793:1e503] labelWidth:728.000000
2013-03-24 18:33:44.010 Bridge Alert[57793:1e503] labelsize:713.000000,76.000000
2013-03-24 18:33:44.010 Bridge Alert[57793:1e503] Pre: <UILabel: 0xad3eaf0; frame = (20 20; 280 21); text = 'Detail'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x17372eb0>>
2013-03-24 18:33:44.011 Bridge Alert[57793:1e503] Post: <UILabel: 0xad3eaf0; frame = (20 22; 728 76); text = 'Detail'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x17372eb0>>
2013-03-24 18:33:44.011 Bridge Alert[57793:1e503] Text set: <UILabel: 0xad3eaf0; frame = (20 22; 728 76); text = 'A bridge is considered “f...'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x17372eb0>>
2013-03-24 18:33:44.014 Bridge Alert[57793:1e503] Display:<UILabel: 0xad3eaf0; frame = (20 20; 728 190); text = 'A bridge is considered “f...'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x17372eb0>>

可以看到,通过Display,标签被调整了大小。我假设以某种方式重新计算高度,基于单元格是否为 320 pt 宽,这是内置的 UITableViewCell 宽度。

如何让标签的尺寸正确?

最佳答案

这就是使用 AutoLayout 使用尽可能少的代码对我有用的方法。

在我的 ViewController 中,我添加了以下方法

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{        
    NSString * yourText = //place here the text you want to calculate its size;
    return 40 + [self heightForText:yourText];
}

-(CGFloat)heightForText:(NSString *)text
{
    NSInteger MAX_HEIGHT = 2000;
    UITextView * textView = [[UITextView alloc] initWithFrame: CGRectMake(0, 0, 320, MAX_HEIGHT)];
    textView.text = text;
    textView.font = [UIFont boldSystemFontOfSize:12];
    [textView sizeToFit];
    return textView.frame.size.height;
}

上面的代码基本上是根据我的 UILabel 的大小 + 一个填充数字(在我的例子中我定义为 40)来更改 UITableViewCell 的大小。

在此之后,我不得不添加一些约束,如下所示,以使 UILabel 像 UITableViewCell 一样“拉伸(stretch)”

enter image description here

运行后:

enter image description here

注意:我从 SO 中的许多线程中提取了不同的代码片段来提出这个解决方案。我希望我能记住所有要在这里提到的 `em

希望对你们有用。

干杯

关于ios - 调整 UILabel 的大小以适应自定义 UITableViewCell 中的文本,无论宽度如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15605853/

相关文章:

ios - Swift - 视差标题 View - ScrollView 重叠单元格

ios - UIActivityViewController 中 UILabel 的 UIAppearance

ios - 由核心数据填充的表无法正确显示

ios - 移动索引 - 基于计时器

ios - Segue 在按下时不会显示 ViewController

ios - 通过模式生成动态文本属性

ios - 如何水平居中 UILabel TEXT?

ios - 使用 ios 5.1.1 时启动时的 SIGABRT

ios - UIView 动画立即发生

iphone - 获取 UITableViewCell 文本的正确方法