Indenting UITextLabel in custom UITableViewCell

206 views Asked by At

I have a custom UITableViewCell and I would like to indent the UITextLabel without indenting the separator's inset.

I tried the following but it didn't yield the result I was looking for. How can I achieve this? Can I edit the auto-layout the UITableViewCell uses? Or do I have to create my own UITextLabel?

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
    if (self) {
       ... 
       self.separatorInset = UIEdgeInsetsZero;
       self.layoutMargins = UIEdgeInsetsZero;
       ...
       self.textLabel.layoutMargins = UIEdgeInsetsMake(self.textLabel.layoutMargins.top,
                  self.textLabel.layoutMargins.left + 30,
                  self.textLabel.layoutMargins.bottom, 
                  self.textLabel.layoutMargins.right);
       }

       return self;

}

Thanks!

1

There are 1 answers

0
Joseph On BEST ANSWER

For anyone looking for the solution to this.

You can override your the auto-layout constraints in the initWithStyle:reuseIdentifier: method like this:

self.textLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-30-[textLabel]" options:0 metrics:nil views:@{ @"textLabel": self.textLabel }]];