Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A simple solution is you can change the frame of the textLabel.</p> <pre><code>CGRect textLabelFrame = cell.textLabel.frame; textLabelFrame.origin.x += xOffset; textLabelFrame.size.width -= xOffset; cell.textLabel.frame = textLabelFrame; </code></pre> <p>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.</p> <p>[EDIT 1/2: fixed typo w/ CGRect]</p> <p>[EDIT 3: fixed typo setting modified frame]</p> <p>[EDIT 4: need a simple subclass]</p> <p>Mea culpa. I was wrong that you can do this in <code>tableView:cellForRowAtIndexPath</code>. The UITableViewCell layout happens after the tableView delegate/datasource has a chance to customize the cell. I've tested the implementation below, it works.</p> <p>Do as I say above but create a (simple) subclass of <code>UITableViewCell</code> that adds the <code>xOffset</code> in <code>layoutSubviews</code>. If you do this, you can also add an <code>xOffset</code> property that you can set in <code>tableView:cellForRowAtIndexPath:</code>.</p> <pre><code>@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; } @end </code></pre> <p>The solution that suggested adding a custom <code>UILabel</code> to <code>cell.contentView</code> is also a good solution. I saw your comment that it obscures the built-in <code>textLabel</code> but that's the whole point. You would not use the built-in label anymore, use the custom label instead.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload