如何包装在一个UITableViewCell文本而不自定义单元格(How do I wrap tex

2019-06-18 14:08发布

这是对iPhone 0S 2.0。 2.1答案是也没关系,虽然我不知道关于表的任何差异。

这感觉就像它应该是可能得到的文本,而无需创建一个自定义的电池包,因为UITableViewCell包含UILabel默认。 我知道我可以,如果我创建一个自定义单元格它的工作,但是这不是我想要实现 - 我想知道为什么我目前的做法是行不通的。

我已经想通了,该标签是按需创建(因为细胞支持文本和图像存取,因此它不创造必要之前的数据视图),所以如果我做这样的事情:

cell.text = @""; // create the label
UILabel* label = (UILabel*)[[cell.contentView subviews] objectAtIndex:0];

然后我得到一个有效的标签,但设置numberOfLines上(和lineBreakMode)不工作-我仍然得到单行文本。 有大量的高度在UILabel的文本显示-我只是在高度返回一个较大的值heightForRowAtIndexPath

Answer 1:

这里有一个简单的方法,它的工作对我来说:

里面你cellForRowAtIndexPath:功能。 当你第一次创建你的:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
}

你会发现,我设置的行数为标签为0,这让它使用尽可能多的行,因为它需要。

接下来的部分是指定你有多大UITableViewCell会,这样做,在你的heightForRowAtIndexPath功能:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText = @"Go get some text for your cell.";
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 20;
}

我加了20到我回来细胞高度,因为我喜欢我的文本周围一点缓冲。



Answer 2:

更新添Rupe的答案iOS7:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText = @"Go get some text for your cell.";
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];

    NSAttributedString *attributedText =
        [[NSAttributedString alloc]
            initWithString:cellText
            attributes:@
            {
                NSFontAttributeName: cellFont
            }];
    CGRect rect = [attributedText boundingRectWithSize:CGSizeMake(tableView.bounds.size.width, CGFLOAT_MAX)
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                               context:nil];
    return rect.size.height + 20;
}


Answer 3:

简评/答案记录我的经验,当我有同样的问题。 尽管使用的代码示例,表视图单元高度为调整,但在细胞内的标签仍然没有正确地调整-溶液,我是从一个自定义的NIB文件,在调整单元格的高度后,这恰好装入我的手机。

我有我的NIB文件中的设置不换行文字,只有1号线的标签; 护理文件设置被重写我的代码内调整设置。

我把这个教训是一定要始终牢记对象的状态是在每个时间点的东西 - 他们可能还没有被创造! ...心连心有人下了线。



Answer 4:

如果我们要在添加纯文本UITableView细胞,我们只需要两名代表一起工作(无需添加额外的UILabels

1) cellForRowAtIndexPath

2) heightForRowAtIndexPath

该解决方案为我工作: -

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{ 
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.textLabel.numberOfLines = 0;

    [cell setSelectionStyle:UITableViewCellSelectionStyleGray]; 
    cell.textLabel.text = [mutArr objectAtIndex:indexPath.section];
    NSLog(@"%@",cell.textLabel.text);

    cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png" ]];

    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{  
    CGSize labelSize = CGSizeMake(200.0, 20.0);

    NSString *strTemp = [mutArr objectAtIndex:indexPath.section];

    if ([strTemp length] > 0)
        labelSize = [strTemp sizeWithFont: [UIFont boldSystemFontOfSize: 14.0] constrainedToSize: CGSizeMake(labelSize.width, 1000) lineBreakMode: UILineBreakModeWordWrap];

    return (labelSize.height + 10);
}

这里的字符串mutArr是从中我得到我的数据的可变数组。

编辑: -在这里是我把数组。

mutArr= [[NSMutableArray alloc] init];

[mutArr addObject:@"HEMAN"];
[mutArr addObject:@"SUPERMAN"];
[mutArr addObject:@"Is SUPERMAN powerful than HEMAN"];
[mutArr addObject:@"Well, if HEMAN is weaker than SUPERMAN, both are friends and we will never get to know who is more powerful than whom because they will never have a fight among them"];
[mutArr addObject:@"Where are BATMAN and SPIDERMAN"];


Answer 5:

现在tableviews可以具有自调整大小的细胞。 设置表视图,如下所示

tableView.estimatedRowHeight = 85.0 //use an appropriate estimate tableView.rowHeight = UITableViewAutomaticDimension

苹果参考



Answer 6:

我用以下解决方案。

数据在一个构件分开设置:

-(NSString *)getHeaderData:(int)theSection {
    ...
    return rowText;
}

该处理可以轻松完成cellForRowAtIndexPath 。 定义单元格/定义字体并指定这些值的结果“小区”。 需要注意的是numberoflines被设置为“0”,这意味着采取所需要的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    UIFont *cellFont = [UIFont fontWithName:@"Verdana" size:12.0];
    cell.textLabel.text= [self getRowData:indexPath.section];
    cell.textLabel.font = cellFont;
    cell.textLabel.numberOfLines=0;
    return cell;
}

heightForRowAtIndexPath ,我计算换行文本的高度。 该粘合大小应与您的单元格的宽度。 为iPad本应是1024对于iPhone的iPod连接320。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIFont *cellFont = [UIFont fontWithName:@"Verdana" size:12.0];
    CGSize boundingSize = CGSizeMake(1024, CGFLOAT_MAX);
    CGSize requiredSize = [[self getRowData:indexPath.section] sizeWithFont:cellFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
    return requiredSize.height;    
}


Answer 7:

我发现这是非常简单明了的:

[self.tableView setRowHeight:whatEvereight.0f];

对于例如:

[self.tableView setRowHeight:80.0f];

这可能是也可能不是这样做的最佳/标准方法,但它在我的情况下工作。



Answer 8:

我尝试在SWIFT代码。 此代码将正常UILabels也行。

extension UILabel {
    func lblFunction() {
        //You can pass here all UILabel properties like Font, colour etc....
        numberOfLines = 0
        lineBreakMode = .byWordWrapping//If you want word wraping
        lineBreakMode = .byCharWrapping//If you want character wraping
    }
}

现在,只需调用这样

cell.textLabel.lblFunction()//Replace your label name 


Answer 9:

我认为这是一个更好的和更短的解决方案。 刚格式化UILabeltextLabel通过指定单元格为高度自动计算) sizeToFit ,一切都应该罚款。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    cell.textLabel.text = @"Whatever text you want to put here is ok";
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.textLabel.numberOfLines = 0;
    [cell.textLabel sizeToFit];

    return cell;
}


Answer 10:

我不认为你可以操纵基地UITableViewCell's私人UILabel做到这一点。 你可以添加一个新UILabel到小区自己和使用numberOfLinessizeToFit它适当的大小。 就像是:

UILabel* label = [[UILabel alloc] initWithFrame:cell.frame];
label.numberOfLines = <...an appriate number of lines...>
label.text = <...your text...>
[label sizeToFit];
[cell addSubview:label];
[label release];


文章来源: How do I wrap text in a UITableViewCell without a custom cell