2

I am trying to center the textLabel in my UITableViewCell with this code:

 cell.textLabel.textAlignment = NSTextAlignmentCenter;

However nothing is changing and the label is still in the left side. Anyone know why?

Abdullah Shafique
  • 6,878
  • 8
  • 35
  • 70

1 Answers1

2

You're using either the subtitle or right detail UITableViewCell style aren't you? I these are the two styles that for what ever reason don't allow you to override the text label's alignment.

To solve this problem, you can make a simple subclass of UITableViewCell with two labels in the same position as they are in the table cell style that you wish to emulate. In fact, to achieve this, the only two lines of code you'd need to write would be the properties for the labels. Then you could do the rest of the work on a prototype cell in Interface Builder.

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • Thanks for your answer! I tried switching to right detail but it still is not working. – Abdullah Shafique Aug 17 '13 at 03:28
  • @AbdullahShafique Ah yes, I'm sorry I forgot about that one. I just made a test project and checked the four cell styles and right detail and subtitle are the only two that don't respond to text alignment change, which personally I think only makes sense for the right detail cell. Still I recommend you subclass. I've edited my answer to reflect this – Mick MacCallum Aug 17 '13 at 03:30