Sheehan Alam
Sheehan Alam

Reputation: 60869

How to make text in a UITableViewCell wordwrap?

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?

Upvotes: 5

Views: 9007

Answers (4)

Vincent
Vincent

Reputation: 4409

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

Upvotes: 6

Paul Lynch
Paul Lynch

Reputation: 19789

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:

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

using:

NSString sizeWithFont:constrainedToSize:

Upvotes: 10

Elfred
Elfred

Reputation: 3851

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.

Upvotes: 2

vodkhang
vodkhang

Reputation: 18741

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

Upvotes: 1

Related Questions