Search code examples
iosrubymotion

How to change the width of the textLabel in a UITableViewCell


I have several UITableViewCell's that have custom buttons in them. These buttons cover up the text in a few of the cells. How do I set the width of the textLabel attribute within the cell so this doesn't happen? This doesn't seem to work.

cell.textLabel.font = UIFont.boldSystemFontOfSize(16)
cell.textLabel.adjustsFontSizeToFitWidth = true
cell.textLabel.numberOfLines = 0
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap
cell.textLabel.preferredMaxLayoutWidth = 200

I am running this in the tableView(table_view, willDisplayCell:cell, forRowAtIndexPath:index_path) method.

Setting the frame at this time doesn't work either.


Solution

  • If you need your UITableViewCells to look better than the default behavior, you should probably draw the complete contents, so your custom buttons and the text and probably other information, in a view subclass of your own and return that as the view for the cell.