iPhone在UITableViewCell中包装文本

Cou*_*son 1 iphone xcode objective-c word-wrap uitableview

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

文字没有包装

我的代码是:

// 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;

}
Run Code Online (Sandbox Code Playgroud)

以下行工作过,但它似乎不起作用.

cell.textLabel.numberOfLines = 0; // Multiline

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

Jam*_*mes 7

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

cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
Run Code Online (Sandbox Code Playgroud)


Mar*_*off 6

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

cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
Run Code Online (Sandbox Code Playgroud)

(未编译,但你明白了).

  • cell.textLabel.numberOfLines = 2 (4认同)