UITableViewCell textLabel offset

I want to have my text in my UITableViewCell to be a little bit to the right. In other words I want to have an x-offset. Is this even possble? Do I have to create a custom cell just because of this?


You can try this :

[cell setIndentationLevel:SOME_NUMBER];
[cell setIndentationWidth:SOME_OTHER_NUMBER];

You could use cell.indentationLevel and if needed cell.indentationWidth instead of custom UITableViewCell for this.

A simple solution is you can change the frame of the textLabel.

CGRect textLabelFrame = cell.textLabel.frame;
textLabelFrame.origin.x += xOffset;
textLabelFrame.size.width -= xOffset;
cell.textLabel.frame = textLabelFrame;

I've also done this by creating a custom UILabel that supports edgeInsets similar to a UIButton. This is a better solution b/c you can layout the label to correct size but the above will work if you have simple needs.

[EDIT 1/2: fixed typo w/ CGRect]

[EDIT 3: fixed typo setting modified frame]

[EDIT 4: need a simple subclass]

Mea culpa. I was wrong that you can do this in tableView:cellForRowAtIndexPath. The UITableViewCell layout happens after the tableView delegate/datasource has a chance to customize the cell. I've tested the implementation below, it works.

Do as I say above but create a (simple) subclass of UITableViewCell that adds the xOffset in layoutSubviews. If you do this, you can also add an xOffset property that you can set in tableView:cellForRowAtIndexPath:.

@implementation XOffsetCell
// assumes property xOffset is defined and synthesized
- (void) layoutSubviews
    [super layoutSubviews];
    CGRect textLabelFrame = cell.textLabel.frame;
    textLabelFrame.origin.x += self.xOffset;
    textLabelFrame.size.width -= self.xOffset;
    cell.textLabel.frame = textLabelFrame;

The solution that suggested adding a custom UILabel to cell.contentView is also a good solution. I saw your comment that it obscures the built-in textLabel but that's the whole point. You would not use the built-in label anymore, use the custom label instead.

I'm not trolling for votes, but wanted to show what the code provided by @iPhone monster "should" look like. His solution is a valid option. If you add the label to the cell after the if (cell == nil) as he did you will be continually adding labels to dequeued cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(50.0f, 10.0f, 150.0f, 20.0f)];
        lbl.tag = OffsetLabelTag; // define this as a constant
        [cell.contentView addSubview:lbl];
        [lbl release];

    UILabel *lbl = (UILabel *)[cell.contentView viewWithTag:OffsetLabelTag];
    [lbl setText:@"test text"];

    return cell;

You can do that with Autolayouts:

UITableViewCell* cell = [UITableViewCell new];
cell.textLabel.translatesAutoresizingMaskIntoConstraints = NO;
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(indentation)-[textLabel]-(indentation)-|"
                                                                         metrics:@{@"indentation": @35}
                                                                           views:@{@"textLabel": cell.textLabel}]];

Or using Parus lib:

[cell.contentView addConstraints:(PVVFL(@"H:|-(indentation)-[textLabel]-(indentation)-|")
                                  .withViews(@{@"textLabel": textLabel})
                                  .metrics(@{@"indentation": @35}).asArray)];

Other way:

    -(NSInteger)tableView:(UITableView *)tableView 
                indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
        return intOffset;  // but is like '\t', not points

There is another not very pretty and smart way : ) Position of Label in TableViewCell depends on size of image assigned to cell.imageView.image property. So if you want your text to move right you can add several blank pixel columns to image in cell.

Will cell.textLabel.textAlignment = UITextAlignmentCenter; be enough for you. It could align the text to the center creating that space for you

Need Your Help

addEventListener for keydown on Canvas

javascript html5 canvas html5-canvas

I am trying to make a canvas app that responds to keyboard and mouse input. I have this code:

How to define hash tables in Bash?

bash dictionary hashtable associative-array

What is the equivalent of Python dictionaries but in Bash (should work across OS X and Linux).