How do I wrap text in a UITableViewCell without a custom cell
Here is a simpler way, and it works for me:
Inside your cellForRowAtIndexPath:
function. The first time you create your cell:
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];
}
You'll notice that I set the number of lines for the label to 0. This lets it use as many lines as it needs.
The next part is to specify how large your UITableViewCell
will be, so do that in your heightForRowAtIndexPath
function:
- (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;
}
I added 20 to my returned cell height because I like a little buffer around my text.
How to wrap text in a UITableViewCell using only IB (no code)?
Here's how I merged the code from the SO answer I mentioned with my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"commentCell";
NSDictionary *comment = [commentsArray objectAtIndex:indexPath.row];
NSString *commentText = [comment objectForKey:@"comment_text"];
NSString *commentAuthor = [comment objectForKey:@"comment_author_name"];
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:14.0];
}
cell.textLabel.text = commentText;
cell.detailTextLabel.text = [NSString stringWithFormat:@"by %@", commentAuthor];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *comment = [commentsArray objectAtIndex:indexPath.row];
NSString *commentText = [comment objectForKey:@"comment_text"];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [commentText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
return labelSize.height + 40;
}
@end
How to wrap text in a UITableViewCell using only IB (no code)?
Here's how I merged the code from the SO answer I mentioned with my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"commentCell";
NSDictionary *comment = [commentsArray objectAtIndex:indexPath.row];
NSString *commentText = [comment objectForKey:@"comment_text"];
NSString *commentAuthor = [comment objectForKey:@"comment_author_name"];
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:14.0];
}
cell.textLabel.text = commentText;
cell.detailTextLabel.text = [NSString stringWithFormat:@"by %@", commentAuthor];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *comment = [commentsArray objectAtIndex:indexPath.row];
NSString *commentText = [comment objectForKey:@"comment_text"];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [commentText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
return labelSize.height + 40;
}
@end
How to make text in a UITableViewCell wordwrap?
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:
Wrap Text in UITableView Custom Cell When It Reaches End of Screen (Swift)
Set label number of "Lines" to ZERO. And set "Line Breaks" to "Word Wrap"
UITableViewCell with custom control in content view + text wrap title
Give height, width, top, and trailing constraints to you ratingControl.
You can't use the default textlabel for this, create a new label to the left of your ratingControl.
Give leading, top and bottom to container margins (important, not to the superview itself but to the view margins, otherwise you can have some autolayout warnings with dynamic height cells), trailing (to the ratingControl, horizontal spacing) and height >= actualHeight contraints to your label.
Then set tableView estimatedRowHeight to the average height of you prototype cells (or 44.0 is good anyway) and tableView.rowHeight = UITableViewAutomaticDimension.
cell!.textLabel!.numberOfLines = 0; is correct.
At this point it should work...
iOS: Adding a third label to UITableViewCell without creating a Custom cell?
See A Closer Look at Table-View Cells, section called Programmatically Adding Subviews to a Cell’s Content View.
mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 220.0, 15.0)] autorelease];
mainLabel.tag = MAINLABEL_TAG;
mainLabel.font = [UIFont systemFontOfSize:14.0];
mainLabel.textAlignment = UITextAlignmentRight;
mainLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview:mainLabel];
Related Topics
Uibutton Action in Table View Cell
How to Test Apple Push Notification Service Without an Iphone
Nsurlsession With Nsblockoperation and Queues
Xcode 9 Error: "Iphone Has Denied the Launch Request"
How to Launch Safari and Open Url from iOS App
Uipageviewcontroller and Storyboard
Uitextfield's Numerical Pad: Dot Instead of Comma for Float Values
Correct Singleton Pattern Objective C (Ios)
How to Convert from Degrees to Radians
How to Run App in Simulator: Xcode Beta 6 iOS 8
How to Support Universal Links in iOS App and Setup Server For It
Iphone App in Landscape Mode, 2008 Systems
How to Hide a Status Bar in Ios
Displaying a Stock iOS Notification Banner When Your App Is Open and in the Foreground