xcode iPhone 在 UITableViewCell 中包装文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6473355/
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 21:20:35  来源:igfitidea点击:

iPhone Wrapping Text In UITableViewCell

iphoneobjective-cxcodeuitableviewword-wrap

提问by Courtney Stephenson

I am trying to create a UITableView cell that says "View this devotional online" with a large font. The issue I am having is the text in the cell is not wrapping so I end up with part of the phrase.

我正在尝试创建一个 UITableView 单元格,用大字体显示“在线查看此灵修”。我遇到的问题是单元格中的文本没有换行,所以我最终得到了部分短语。

text not wrapping

文字不换行

The code I have for this is:

我的代码是:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

// Get cell
static NSString *CellIdentifier = @"CellA";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

// Display
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.font = [UIFont systemFontOfSize:15];
if (item) {

    // Item Info
    NSString *itemTitle = item.title ? [item.title stringByConvertingHTMLToPlainText] : @"[No Title]";

    // Display
    switch (indexPath.section) {
        case SectionHeader: {

            // Header
            switch (indexPath.row) {
                case SectionHeaderTitle:
                    cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
                    cell.textLabel.text = itemTitle;
                    break;
                case SectionHeaderDate:
                    cell.textLabel.text = dateString ? dateString : @"[No Date]";
                    break;
                case SectionHeaderURL:
                    cell.textLabel.text = @"View this devotional in full website";
                    cell.textLabel.textColor = [UIColor blackColor];
                    cell.textLabel.font = [UIFont systemFontOfSize:36];
                    cell.imageView.image = [UIImage imageNamed:@"Safari.png"];
                    cell.textLabel.numberOfLines = 0; // Multiline
                    break;
            }
            break;

        }
        case SectionDetail: {

            // Summary
            cell.textLabel.text = summaryString;
            cell.textLabel.numberOfLines = 0; // Multiline
            break;

        }
    }
}

return cell;

}

The following line worked before, but it doesn't seem to work here.

以下行以前有效,但在这里似乎不起作用。

cell.textLabel.numberOfLines = 0; // Multiline

cell.textLabel.numberOfLines = 0; // Multiline

Could someone show me how to wrap my text?

有人可以告诉我如何包装我的文字吗?

回答by James

As of iOS6, UILineBreakModeWordWrap is deprecated. You'll need-

从 iOS6 开始,不推荐使用 UILineBreakModeWordWrap。你需要-

cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;

回答by Mark Granoff

In addition to setting numberOfLinesto zero, set the line break mode:

除了设置numberOfLines为零之外,还要设置换行模式:

cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

(uncompiled, but you get the idea).

(未编译,但你懂的)。