How can I change a cell's textLabel frame?

22,966

Solution 1

The only way to do this is to use a UITableViewCell subclass and override -layoutSubviews. In this method, you'll want to call [super layoutSubviews], and then do any frame tweaks that you want to the label.

Solution 2

override layoutSubviews for your UITableViewCell...

- (void)layoutSubviews {
    [super layoutSubviews];

    CGSize size = self.bounds.size;
    CGRect frame = CGRectMake(4.0f, 4.0f, size.width, size.height); 
    self.textLabel.frame =  frame;
    self.textLabel.contentMode = UIViewContentModeScaleAspectFit;
}

or you can simply make your own UILabel object, and add it to cell.contentView as a subview.

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 30, 30)];
[cell.contentView addSubview:label];
[label release];

Solution 3

I ended up adding a \n in the beginning of the string. Unfortunately I couldn't get anything else to work.

Share:
22,966
Raphael Caixeta
Author by

Raphael Caixeta

Web & iOS Developer. @gripd co-founder. #crossfitter. Adrenaline rush junkie. Brazilian. All around nerd, overall awesome.

Updated on July 23, 2022

Comments

  • Raphael Caixeta
    Raphael Caixeta almost 2 years

    I've tried nearly everything, but I just can't seem to move the cell.textLabel property down a little bit. I've attached a screenshot below and I've tried nearly everything I could find.

    I've tried changing the .frame property directly, attempted at modifying if via using the "- (void)tableView:(UITableView *)tableView willDisplayCell" method". I've also tried allocating a custom label. I could move the custom label, but it wouldn't go into separate lines like the original textLabel. I just need to move the pictured multi line label a bit down.

    Any help is appreciated!

    enter image description here

  • Mel
    Mel over 8 years
    Could you explain what your code does, and why it answers the question ?