Milad
Milad

Reputation: 1269

iOS - UITableViewCell text alignment

I've added a tableView and dragged a table view cell into it. in the utilities panel, I changed the style to subtitle. I've also tried changing it in code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

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

    cell.textLabel.textAlignment = UITextAlignmentCenter;
    cell.detailTextLabel.textAlignment = UITextAlignmentCenter;

    cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [myArray2 objectAtIndex:indexPath.row];

    return cell;

The center alignment doesn't work!

I've tried adding a label object to the cell to have a workaround. But I don't know how to access it. even though I assigned an outlet to it, this wouldn't work:

cell.labelForCell....

What should I do? any suggestions on how I make it work the usual way, without adding a label to the cell or something?

Upvotes: 7

Views: 20731

Answers (6)

Augustin A
Augustin A

Reputation: 127

Hi please add the following instead of your code,

cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.detailTextLabel.textAlignment = UITextAlignmentCenter;

change to

cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.detailTextLabel.textAlignment = NSTextAlignmentCenter;

it will work fine.

Upvotes: 0

malex
malex

Reputation: 10096

It is impossible to change frame for textLabel and detailTextLabel in UITableViewCell right in cellForRowAtIndexPath: method.

If you don't want to subclass your cell you can implement a small hack using

performSelector:withObject:afterDelay:

with zero delay for changing geometry right after default layoutSubviews:

[self performSelector:@selector(alignText:) withObject:cell afterDelay:0.0];

See details at here

Upvotes: 0

Sanjeev Rao
Sanjeev Rao

Reputation: 2297

I think adjusting the frame will work. I had style of UITableViewCellStyleValue2 but If i have a bit lengthy text in textLabel, it getting truncated at tail and textAlignment does not work here, so thought of increase the width textLabel.

 -(void)layoutSubviews{
        [super layoutSubviews];

        if ([self.reuseIdentifier isEqualToString:@"FooterCell"]) {
            CGRect aTframe  = self.textLabel.frame;
            aTframe.size.width += 40;
            self.textLabel.frame = aTframe;

            CGRect adTframe  = self.detailTextLabel.frame;
            adTframe.origin.x += 70;
            self.detailTextLabel.frame = adTframe;        
        }
    }

Upvotes: 0

Ian Hoar
Ian Hoar

Reputation: 931

The problem with the subtitle style is that it does a [self.textLabel sizeToFit] when it lays out. When you center in a container that is the perfect size of the contents, nothing changes.

Try this. In a subclass of UITableViewCell, set your textAlignment, and use this as your layoutSubviews code:

- (void)layoutSubviews
{
    [super layoutSubviews];

    {
        CGRect frame = self.textLabel.frame;
        frame.size.width = CGRectGetWidth(self.frame);
        frame.origin.x = 0.0;
        self.textLabel.frame = frame;
    }

    {
        CGRect frame = self.detailTextLabel.frame;
        frame.size.width = CGRectGetWidth(self.frame);
        frame.origin.x = 0.0;
        self.detailTextLabel.frame = frame;
    }
}

This makes the textLabel's frame full width, and thus allows the centering effect to be noticeable.

Note: since this overrides layoutSubviews, there is a performance cost as it will be called often.

Upvotes: 2

Siam
Siam

Reputation: 109

I think the reason is that: the textLabel's width depends on the text length, if the text is too long to show all in a signal line, and you have already set the line break mode and set the number of lines to 0, you will find that the text alignment will be work.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *identifier = @"identifier";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (nil == cell)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];
        cell.textLabel.textAlignment = UITextAlignmentLeft;
        cell.textLabel.textColor = [UIColor redColor];
        cell.detailTextLabel.textColor = [UIColor greenColor];
        cell.detailTextLabel.textAlignment = UITextAlignmentCenter;
        cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
        cell.textLabel.numberOfLines = 0;
    }
    cell.textLabel.text = [NSString stringWithFormat:@"You can initialize a very long string for the textLabel, or you can set the font to be a large number to make sure that the text cann't be shown in a singal line totally:%d", indexPath.row];
    return cell;

}

Upvotes: 0

Omar Abdelhafith
Omar Abdelhafith

Reputation: 21221

For the UITableViewCellStyleSubtitle text alignment cannot be changed You will have to put a label and add your alignment to it, To do that you could use this code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UILabel *myLabel;
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        myLabel = [[UILabel alloc] initWithFrame:Your_Frame];
        //Add a tag to it in order to find it later
        myLabel.tag = 111;
        //Align it
        myLabel.textAlignment= UITextAlignmentCenter;

        [cell.contentView addSubview:myLabel];
    }

    myLabel = (UILabel*)[cell.contentView viewWithTag:111];
    //Add text to it
    myLabel.text = [myArray objectAtIndex:indexPath.row];

    return cell;
}

Upvotes: 19

Related Questions