Wednesday, February 11, 2009

Centering Text In UITableViewCell (iPhone SDK)

Unfortunately the iPhone SDK 2.2 has a bug that prevents centering UITableViewCell text using:

cell.textAlignment = UITextAlignmentCenter;


Here is a work-around:

UILabel* label = [[[cell contentView] subviews] objectAtIndex:0];
label.textAlignment = UITextAlignmentCenter;

4 comments:

Tyler Bye said...

Was poking around the net and found this page. The update for folks who may come across it is to set textAlignment on the property of the label of the UITableViewCell, or in other code words:

UITableViewCell *cell; //abbreviated for comment

cell.textLabel.textAlignment = UITextAlignmentCenter;

Cheers!

Anonymous said...

Thanks a lot for this tip Tyler! It works great!

Anonymous said...

Hm, this doesn't work in 4.3. There are some properties that inexplicably can't be set when you return the cell, but instead must be set in the delegate's willDisplayCell method.

Anonymous said...

Thanks a lot, it worked for me