如何在自定义表格单元类中绘制矩形?该单元格当前有一个带有一些文本标签的背景图像。我想在每个标签后面画一个矩形,以便更容易阅读详细的背景图像。
我知道我可以只设置标签的背景颜色,但我想在背景颜色和文本之间进行填充。如果可能的话,我很想知道怎么做!:)
我在 Three20 中对 TTTableMessageItemCell 进行子类化,调用下面的方法,您可以在其中使用单元格的子视图,
- (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;
}
我希望将矩形放置在 _titleLable 和 detailTextLabel 标签后面。
编辑 我已经能够使用以下内容添加正确的框,
UIView *view = [[UIView alloc] init]; view.backgroundColor = [UIColor whiteColor]; view.frame = CGRectMake(padding, top, textWidth, textHeight+2*padding); [self insertSubview:view belowSubview:self.detailTextLabel];
它位于标签之上,我似乎无法将它放在它后面......
编辑 我将视图添加到错误的子视图,修复它,
[[self.subviews objectAtIndex:0] insertSubview:view atIndex:0];