xcode 如何在 UITableViewCell 自动换行中制作文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2906090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:23:23  来源:igfitidea点击:

How to make text in a UITableViewCell wordwrap?

iphoneobjective-ccocoa-touchxcodeuitableview

提问by Sheehan Alam

I have a grouped UITableView with a few sections. The text in some of the cells can get quite long, I was wondering how I could make the text word-wrap?

我有一个包含几个部分的分组 UITableView。某些单元格中的文本可能会很长,我想知道如何使文本自动换行?

回答by Paul Lynch

eg

例如

textLabel.numberOfLines = 0;

You can set this to a fixed number of lines if you prefer. It may be a good idea to set the lineBreakMode, and you will probably need to implement:

如果您愿意,可以将其设置为固定的行数。设置 lineBreakMode 可能是个好主意,您可能需要实现:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

using:

使用:

NSString sizeWithFont:constrainedToSize:

回答by Vincent

You'll have the specify the number of lines the text will use. Setting it to zero will wrap it automatically.

您将指定文本将使用的行数。将其设置为零将自动包装它。

cell.textLabel.numberOfLines=0; // line wrap

回答by Elfred

set numberOfLines > 1 on your label and set an appropriate lineBreakMode as well. You might need to modify the frame of the label to have enough space. You can do this in tableView:willDisplayCell:forRowAtIndexPath: of your table view delegate.

在标签上设置 numberOfLines > 1 并设置适当的 lineBreakMode。您可能需要修改标签的框架以获得足够的空间。您可以在 tableView:willDisplayCell:forRowAtIndexPath: 的表格视图委托中执行此操作。

回答by vodkhang

It looks like this question Custom Cell Height, right? I don't really get what word-wrap do, but I assume that you want to change the cell size according to the text length, is that right? So you can look at the above question and answer

看起来像这个问题Custom Cell Height,对吧?我真的不明白自动换行的作用,但我假设您想根据文本长度更改单元格大小,对吗?所以你可以看看上面的问答