0

How would I draw a rectangle in a custom table cell class? The cell currently has a background image with a few text labels. I would like to draw a rectangle behind each of the labels so they are easier to read over the detailed background image.

I know I could just set the background colour of the label but I would like to have padding between the background colour and the text. If that is possible, I'd love to know how! :)

I'm subclassing a TTTableMessageItemCell in Three20, a method below gets called in which you can play with subviews of the cell,

- (void)layoutSubviews {

[super layoutSubviews];

CGFloat padding = 16;
CGFloat boxWidth = self.contentView.width - 2*padding;
CGFloat textWidth = boxWidth - (padding*2);
CGFloat textHeight = 100;
CGFloat top = kTableCellSmallMargin;

// Position Heading Text
_titleLabel.frame = CGRectMake(padding, top, textWidth, _titleLabel.font.ttLineHeight);
top += _titleLabel.height;

// Position Detail Text
[self.detailTextLabel sizeToFit];
self.detailTextLabel.top = top+2*padding;
self.detailTextLabel.left = 2*padding;
self.detailTextLabel.width = textWidth;
self.detailTextLabel.height = 100;    
}

I would like the rectangles to be placed behind the _titleLable and detailTextLabel labels.

edit I have been able to add the right box using the following,

UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor whiteColor];
view.frame = CGRectMake(padding, top, textWidth, textHeight+2*padding);
[self insertSubview:view belowSubview:self.detailTextLabel];

It is laying on top of the label and I cant seem to get it behind it...

edit I was adding the view to the wrong subview, fixed it with,

[[self.subviews objectAtIndex:0] insertSubview:view atIndex:0];
TimOz44
  • 11
  • 1
  • 6

1 Answers1

1

You can add the labels to views and these to the cell.

You could use insertSubview:belowSubview: to add views behind your labels. With backgroundColor and the right frame they will do what you intend to.

You can also bring detailLabel to front

Zoe
  • 27,060
  • 21
  • 118
  • 148
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178