Share on Google+Share on Google+

UITableViewCell Wrap Text - an Example

Advertisement
In the example, we are discussing about the setting of UITableViewCell to warp the text in multiple lines. Though you can do the setting directly through nib, but in the example we are doing it programatically.

UITableViewCell Wrap Text - an Example

In the example, we are discussing about the setting of UITableViewCell to warp the text in multiple lines. Though you can do the setting directly through nib, but in the example we are doing it programatically.

UILineBreakMode is the property of UITableViewCell that is used to break the text or wrap the text in multiple lines. We are using the UILineBreakModeWordWrap property in the example.

List of other available UILineBreakMode properties:

- UILineBreakModeWordWrap = 0, // Wrap at word boundaries
- UILineBreakModeCharacterWrap, // Wrap at character boundaries
- UILineBreakModeClip, // Simply clip when it hits the end of the rect
- UILineBreakModeHeadTruncation, // Truncate at head of line: "...wxyz". Will truncate multiline text on first line
- UILineBreakModeTailTruncation, // Truncate at tail of line: "abcd...". Will truncate multiline text on last line
- UILineBreakModeMiddleTruncation, // Truncate middle of line: "ab...yz". Will truncate multiline text in the middle

a Simple Example:

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

// set font style, modes & lines.
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:12.0];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 3; // 0 means no max.


}

// Configure the cell.
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

return cell;
}

the Output is:

Download Code

Advertisements

Advertisement

Posted on: September 28, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.